Geeks With Blogs
Information -> Source for Next Generation brcraju@hotmail.com

Learn how to write a Regular Expression:
---------------------------------------------------------------

What Regular Expression?
A regular expression is a pattern that can match various text strings, used for validations.

Where and when to use Regular Expression?
It can be used in the programming languages which supports or has regular expression class as in built or it supports third party regular expression libraries.

Regular expressions can be used to valid different type of data without increase the code with if and case conditions. A number of if conditions can be omitted with single line of regular expression checking.

Benefits of Regular Expression:
The following are benefits (not all included) of use of Regular Expression.
a) # line of code can be reduced.
b) Speed Coding.
c) Easy maintenance (you don’t need to change if validation criteria changes, just check the regular expression string).
d) Easy to understand (you don’t need to understand the programmer logic on large if statements and case statements).

Elements of Regular Expression:
Here are the basic elements of regular expression characters/literals, which can be used to build big regular expressions:

^ ---->Start of a string.
$ ---->End of a string.
. ----> Any character (except \n newline)
{...}----> Explicit quantifier notation.
[...] ---->Explicit set of characters to match.
(...) ---->Logical grouping of part of an expression.
* ---->0 or more of previous expression.
+ ---->1 or more of previous expression.
? ---->0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string.
\ ---->Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below.
\w ----> matches any word character, equivalent to [a-zA-Z0-9]
\W ----> matches any non word character, equivalent to [^a-zA-Z0-9].
\s ----> matches any white space character, equivalent to [\f\n\r\v]
\S----> matches any non-white space characters, equivalent to [^\f\n\r\v]
\d ----> matches any decimal digits, equivalent to [0-9]
\D----> matches any non-digit characters, equivalent to [^0-9]

\a ----> Matches a bell (alarm) \u0007.
\b ----> Matches a backspace \u0008 if in a [] character class; otherwise, see the note following this table.
\t ---->Matches a tab \u0009.
\r ---->Matches a carriage return \u000D.
\v ---->Matches a vertical tab \u000B.
\f ---->Matches a form feed \u000C.
\n ---->Matches a new line \u000A.
\e ---->Matches an escape \u001B

$number ----> Substitutes the last substring matched by group number number (decimal).
${name} ----> Substitutes the last substring matched by a (? ) group.
$$ ----> Substitutes a single "$" literal.
$& ----> Substitutes a copy of the entire match itself.
$` ----> Substitutes all the text of the input string before the match.
$' ----> Substitutes all the text of the input string after the match.
$+ ----> Substitutes the last group captured.
$_ ----> Substitutes the entire input string.

(?(expression)yes|no) ----> Matches yes part if expression matches and no part will be ommited.


Simple Example:
Let us start with small example, taking integer values:
When we are talking about integer, it always has fixed series, i.e. 0 to 9 and we will use the same to write this regular expression in steps.

a) Regular expression starts with “^”
b) As we are using set of characters to be validated, we can use [].
c) So the expression will become “^[1234567890]”
d) As the series is continues we can go for “-“ which gives us to reduce the length of the expression. It becomes “^[0-9]”
e) This will work only for one digit and to make it to work for n number of digits, we can use “*”, now expression becomes “^[0-9]*”
f) As with the starting ending of the expression should be done with “$”, so the final expression becomes “^[0-9]*$”

Note: Double quotes are not part of expression; I used it just to differentiate between the sentences.

Is this the way you need to write:
This is one of the way you can write regular expression and depending on the requirements and personal expertise, regular expression could be compressed much shorter, for example above regular expression could be reduced as.

a) Regular expression starts with “^”
b) As we are checking for the digits, there is a special character to check for digits “\d”
c) And digits can follow digits , we use “*”
d) As expression ends with “$”, the final regular expression will become
"^\d*$”

Digits can be validated with different ways of regular expressions:

1) ^[1234567890]*$
2) ^[0-9]*$
3) ^\d*$

Which one to choose?
Every one of above expressions will work in the same way, choose the way you are comfort, it is always recommended to have a smaller and self expressive and understandable, as these will effect when you write big regular expression.

Example on exclude options:
There are many situation which demands us to exclude only certain portion or certain characters,
Eg: a) Take all alpha numeric and special symbols except “&”
b) Take all digits except “7”
then we cannot prepare a big list which includes all instead we use the symbol of all and exclude the characters / symbols which need to be validated.
Eg: “^\w[^&]*$” is the solution to take all alpha numeric and special symbols except “&”.

Other Examples:
a) There should not be “1” as first digit,?
^[^1]\d*$ ? this will exclude 1 as first digit.

b) There should not be “1” at any place?
^\d[^1]*$ ? this will exclude the 1 at any place in the sequence.

Note: Here ^ operator is used not only to start the string but also used to negate the values.

Testing of Regular expression:
There are several ways of testing this
a) You can write a windows based program.
b) You can write a web based application.
c) You can even write a service based application.


Windows base sample code:
Here are steps which will be used for regular expression checking in dotNet:

a) Use System.Text.RegularExpression.Regex to include the Regex class.
b) Create an Regex object as follows:
Regex regDollar= new System.Text.RegularExpressions.Regex("^[0-9]*$ ");
c) Call the IsMatch(string object) of the Regex call, which will return true or flase.
d) Depending on the return state you can decide whether passed string is valid for regular expression or not.]

Here is the snap shot code as function:

Public boolean IsValid(string regexpObj, string passedString)
{
//This method is direct method without any exceptional throwing..
Regex regDollar= new System.Text.RegularExpressions.Regex(regexpObj);
return regDollar.IsMatch(passedString);
}

With minor changes to the above function it can be used in windows or webbased or even as a service.

Another way -- Online checking:
At last if you are fed up with above and you have internet connection and you don’t have time to write sample, use the following link to test online

http://www.regexplib.com/RETester.aspx


MORE INFO:
You can find more information on these type of characters at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterescapes.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterclasses.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpcongroupingconstructs.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterclasses.asp


--Here is the end of article, hope this basic build will definetely useful for writing a big and good Regular Expression ---

Express your code with REGULAR EXPRESSIONS :))

Posted on Thursday, October 23, 2003 10:39 AM | Back to top


Comments on this post: Learn how to write a Regular Expression:

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
An excellent article. Great writeup Raju

Some of the finer points I believe you can add to this article are.

1. The input is considered to be text which is parsed to return the matches.
2. The ^ regex literal matches the start of a LINE of input
3. The $ literal matches the end of a LINE of input
4. Hence the example about integers which you have mentioned would not match any thing other than to validate if a given input is of a numeric word as the complete input or not.

If the input is "I am 123" it will not match the input / return to you 123 in the input.
It will only return matches for inputs like "123" "234"

So if you are trying to convey something like "This pattern is going to return to you all the numbers in the given input" (I understand it that way after reading through your article) then it should be modified as \d* thats more than enf.

Another point I would like you to add is you can ask people who are new to regex to actually use tools like Expresso (http://www.codeproject.com/dotnet/Expresso.asp) using which you can build expressions and test them immediately (the beauty of this tool is that it uses plain english to help you build the expression) a must have for a developer who is new to regex.

Regards,
Ansari
Left by Tameem Ansari on Oct 25, 2003 7:45 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Thanks Ansari for your input, I defintely agree with your four points, I tried to concentrated on easy way of understanding and this could be applied with small set of character's, like integers, and you are right I concentrated mostly on nemeric, except in the section

"Example on exclude options"

where I have given example for exclude of & from alpha numeric string. at the end of the string it has printed some junk

“&”

actually it is & within double quotes, I think site is not handling that part.

I should have mentioned this tool, the tool (Expresso) is cool and very much useful to play for the beginners, Thanks for reminding.
Left by Ramchander on Oct 27, 2003 6:33 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
DotNetNuke 2.2
Left by BangTech, Inc. on Oct 05, 2004 3:59 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
An very very good article!

By the way, could you tell me how to validate data field with supporting unicode.
For example, I want to check the input name with only characters (a-z, A-Z, 0-9).
So, I created the RegExp: ^\[a-zA-Z0-9]*$.
When I input the string, e.g. "Smith", it is Ok. But when I put the
"Freinke's Tê", The Regex didn't work? Do you have any ideas?

Thanks so much!
Left by Nghia Ngo on Mar 11, 2005 12:28 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Nghia Ngo,

For your example you have used special character Single quote as an input, which you have not included in your regular expression, and with respect to unicode supporting.

Look at the following lines from unicode organization:

Unicode is a large character set—regular expression engines that are only adapted to handle small character sets will not scale well.
Unicode encompasses a wide variety of languages which can have very different characteristics than English or other western European text.

The following link breifs outline on how you can write for your purpose with regarding to unicode, they have given brief with example:

http://www.unicode.org/reports/tr18/tr18-9.html


Left by Ramchander on Mar 11, 2005 8:55 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi, there usefull article. Thanks.

Is it possible to check the following expression:
1. Matches any word ([a-zA-Z0-9])
2. Quatified: from 4 to 10 excluding a, b, and c words.

Thank you
Left by Ruslan on May 10, 2005 8:19 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I've used this RegEx to find HTML remarks, even if they contain tags:

"(<!--(?>[^<>]+|<(?<NEST>)|>(?<-NEST>))*(?(NEST)(?!))>)"

The only thing is, I want to ignore RTF colour tags but still match the
rest of the expression. To find RTF colour tags I use:

"(\\cf\d{1})"

(Which matches '\cf2, \cf1 etc'). I cannot match it into its own group.
Is their a RegEx for exclusive matching?

OK, this maybe deep-end so I might have to settle for RegEx.Split...
Left by Dan on May 16, 2005 2:51 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I am trying to validate an email address but would like to exclude certain domains (e.g. hotmail, yahoo etc.). I am currently using the foll. but it doesn't seem to work.


" *\\w+([-+.]\\w+)*@[^(hotmail|yahoo)]\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)* *"

Could you help? Thanks!

Nirmal
nirmal.parikh@comcast.net
Left by Nirmal on May 26, 2005 12:31 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
How do I match double quotes since I am specifying the pattern inside the quotes?

It doesn't like it when I try to escape it:

For example, matching on a word inside quotes like below does not work.

RegExp myReg = new RegExp("\"w+\"");

Left by Ali on Jun 21, 2005 1:21 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,
I have an email validation, but it does not allow hyphens in the domain name. I have tried inserting '-' into the [ ] like this: [-A-Za-z], but it still returns false. How do I allow hypens??

if (str.match(/^(.*|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/) == null)
{
alert("The e-mail address seems incorrect.");
return false;
}
Left by Shane on Jun 23, 2005 4:57 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
how to write a regular expression for (php)a password ?
password must have a capital letter, a digital, a small letter and no( ibm, sun, hp) maximum length is 16.

Thanks
Left by george on Jun 28, 2005 3:17 PM

# Learn how to write a Regular Expression:
Requesting Gravatar...
TrackBack From:http://cy163.cnblogs.com/archive/2005/07/29/202659.html
Left by cy163 on Jul 28, 2005 8:58 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,
I need val exp that the number should be >0

0.0 invalid
0 is invalid

0.01 valid ...

1 valid ...

Thanks
Jay
Left by Jay Dubal on Jul 30, 2005 8:40 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi how do i write a regular expression to find " [ ] " box brackets, these brackets may contain any text. I want to find this brackets in a given string.

Any inputs regarding thing will be very helpful.

Thanks
Vikram
Left by Vikram on Oct 18, 2005 10:56 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
How to write a regulat expression to split a string of length 100 characters into 10 strings of 10 characters each???

Thank You in advance...

Dheeraj
Left by Dheeraj Kumar on Oct 27, 2005 8:15 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,
Useful info.
I am new to writing regular expressions.
I need to write regular expression for the if<condition>
The <condition> can be anything. may be the syntax allowed in VB.
Can you please tell me a better way to write the regular expression.

Thanks in advance,
Priya.
Left by Priya on Nov 29, 2005 12:41 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Does any body have a solution to Nirmal's problem posted above. i am facing with the same issue. Please help..

I am trying to validate an email address but would like to exclude certain domains (e.g. hotmail, yahoo etc.). I am currently using the foll. but it doesn't seem to work.


" *\\w+([-+.]\\w+)*@[^(hotmail|yahoo)]\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)* *"

Could you help? Thanks!
Kamal
kamal.ramesh@gmail.com
Left by Kamal Ramesh on Jan 06, 2006 11:37 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi guys, am tryin to write an If Statement for the new Visual studio express. Pls how can i do it.
Left by Adams Olu on Jan 27, 2006 2:35 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
i urgently need a regular expression for accepting a value from a user such that it should accept both integer values (123) and decimal values but not more than two places after decimal should be there(123.45).

please help

thanks and regards
Left by Ravish Jain on Feb 15, 2006 4:31 PM

# Regular Expression for number both integers and with decimal
Requesting Gravatar...
i urgently need a regular expression for accepting a value from a user such that it should accept both integer values (123) and decimal values but not more than two places after decimal should be there(123.45).

please help

thanks and regards
Left by Ravish Jain on Feb 15, 2006 4:35 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I want the regular expresssion code for using in htaccess file to redirect to somefile.html
Left by vikas on Apr 07, 2006 4:12 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,
Could u plz help me in getting regular expression for the following conditions:

1)Length of a serial key must be 9 to 11
2)Number of numeric charecter in that key must be 8 or 9
3)If the lenght of the key is 11 then numeric charecters cannot be 8 in number
4)If the serial key is of length 9 then it must not contain something other that alphaneumeric

Since the requirement is very urgent, plz give me the solution in terms of Regular Expression.
By the awy, I am using RegularExpressionValidator in ASP.Net in Visual Studio 2003.
I will be using those regular expressions there.

Thanks in advance,
Aditya N H
Left by Aditya N H on Apr 13, 2006 11:47 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
very good and very fast. Very useful for a kick start with regular expressions.

can you please tell me if the regular expression i ve written is right??

layoutarea_<name>_type : ^layoutarea_[a-zA-Z0-9_]*_type$
technicalarea_static<Number>_type :^technicalarea_static[0-9]*_type$
Left by Meghana Randad on May 22, 2006 3:57 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Is possible something like:

regex rg = new regex ("[Any of Values of Array from Database]");

in which "Any of Values of Array from Database" is a list of rows
extracted by a column?
I know is a "curious replace of a normal Stored p.",but may be
more versatile in combo with some "reducing" patterns combo..

Thank You So much!
Great Blog!
Left by Frank Drebin on Mar 04, 2007 8:15 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,

I want Create New Extraction Rules Type for Pattern Based Rule

for (xxx) xxxx-xxxx-xxxx

how to setting pls ans


thk
elaine
Left by Elaine Hui on May 07, 2007 1:56 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,
I want a regular Expression which can validate a textbox to allow a valid IP-Address or Domain Name System(DNS). User can enter any one of the both, it can be either IP-Address or the DNS. DNS should start with an alphabet but can end with numeric digits also.

Please reply as soon as possible.

Thans in advance.
Syed Saleem
Left by Syed Saleem on Jun 26, 2007 10:14 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
not a useful link.
Left by testuser on Jul 05, 2007 3:56 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
hi see this regulare expression topics
Left by Gurunathan on Aug 24, 2007 4:53 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
how can one write a regular expression on a set of string with an equal number of o's and 1's such that no prefix has two more 0's than 1's, nor two more 1's than 0's
Left by olu on Sep 26, 2007 7:39 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Very useful.Great work.

Thanx n rgds
Prince D
Left by prince on Oct 08, 2007 10:05 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
This is a wonderful resource! I am very happy to have found it. It already helped me with a big issue at work. THANKS!!!
Left by Jason Green on Nov 26, 2007 2:10 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,
I want ot validate following url :

http://geekswithblogs.net/brcraju/articles/235.aspx

i want to allow 'brcraju/articles/235.aspx this . Can any one tell me the proper regular expression


Thank You.
'
Left by girish on Nov 27, 2007 8:41 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I have some HTML like:

<p class="MsoNormal" style="margin: 0in 0in 0pt; vertical-align: middle; line-height: 10pt; mso-pagination: none; mso-hyphenate: none; mso-layout-grid-align: none"><span style="font-size: 10pt; color: black; font-family: 'Times','serif'; letter-spacing: 0.4pt; mso-bidi-font-family: 'Times New Roman'">Did you know that 2007 was actually the fifth highest year for home sales in history? Take a </span><span style="font-size: 10pt; color: black; font-family: 'Times','serif'; letter-spacing: 0.4pt; mso-bidi-font-family: 'Times New Roman'">closer look at the numbers and you will discover that the state of the real estate market may not be everything the media leads you to believe. Sometimes we all need a little historical perspective to see the big picture clearly</span>&nbsp;</p>

Now I want line-height property to be removed from this HTML using Regular Expression. Can anybody help me?

Remember there can be line-height: 10pt; line-height: 9pt; or line-height: 2in;
Left by Mayank Parmar on Dec 20, 2007 12:15 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hello
I urgently require a regular expression for writing all the city names in the world.The name can contain white space like ex Abu dhabi or New York and it can even be 1 single word like london or mumbai.How to write a regular expression for this.Please help.thanks in advance.
Left by tanu on Dec 26, 2007 3:23 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I need help searching multiple files (txt files) that contain a dump of the ipconfig /all command.

I need to exract the ip address and the host name located in these files.

Here is an example of the data in the said files.

Windows IP Configuration



Host Name . . . . . . . . . . . . : computername

Primary Dns Suffix . . . . . . . : domain

Node Type . . . . . . . . . . . . : Hybrid

IP Routing Enabled. . . . . . . . : No

WINS Proxy Enabled. . . . . . . . : No

DNS Suffix Search List. . . . . . : domain

domain



Ethernet adapter Local Area Connection:



Connection-specific DNS Suffix . : domain

Description . . . . . . . . . . . : Broadcom NetXtreme Gigabit Ethernet

Physical Address. . . . . . . . . : 00-00-00-00-00-00

Dhcp Enabled. . . . . . . . . . . : Yes

Autoconfiguration Enabled . . . . : Yes

IP Address. . . . . . . . . . . . : 192.0.0.0

Subnet Mask . . . . . . . . . . . : 192.0.0.0

Default Gateway . . . . . . . . . : 192.0.0.0

DHCP Server . . . . . . . . . . . : 192.0.0.0

DNS Servers . . . . . . . . . . . :

192.0.0.0

Primary WINS Server . . . . . . . : 192.0.0.0

Secondary WINS Server . . . . . . : 192.0.0.0

Lease Obtained. . . . . . . . . . : Tuesday, February 05, 2008 1:22:23 PM

Lease Expires . . . . . . . . . . : Friday, February 08, 2008 1:22:23 PM


I have been trying to use sed and awk. At the moment Im playing around with data extractor 3.3 to see if thats more helpful.

Anyone got any suggestions?


Sj
Left by Steven Justice on Feb 07, 2008 10:40 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Someone mentionned that earlier, but I don't get how you can make a pattern for double quotes. Something like that won't work for instance: '/"/' while this works: '/f/' matching every f letters.
Left by 1up on Feb 26, 2008 5:56 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
My bad, it works. The double quote had been converted to an html entity.
Left by 1up on Feb 26, 2008 6:03 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hey
How would you suggest to validate the next serial number?

(110A or 120A, or 130A)-(any 4 digits hex number)-(any 4 digits hex number)-(any 4 digits hex number)

so, the serial is 16 digits long,. starting with 110A or 120A or 130A , and each groupd of 4 hex digits is seperated by "-"

Thanks for the help!

Left by dash on Mar 06, 2008 9:37 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Great article, thanks.

To all the people who replied "please write a regular expression for me to do [insert job here]" - how about figuring out something yourself gang? I doubt your boss is really willing to pay you all to copy and paste code from internet articles in between long Youtube sessions and shopping sprees at your desk each day.

In other words - don't just copy code - learn to program.
Left by Jim on Mar 06, 2008 11:57 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Good Article. I want to write a regular expression for matching only first 5 bytes of a pattern. Say if the pattern in 9&watchthenet , i want to match only 9&wat. How can I do it?
Left by universe on Mar 16, 2008 4:11 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I want to extract data from text files between two parameters such as:

Start: BEGIN LONG DESCRIPTION
End: END LONG DESCRIPTION

There assorted text in between.

It would also be handy to be able to enter several paramaters at once so I can grab all the data in one go - there are thousands of records to deal with.

Any help greatly appreciated
Left by Michael on Mar 28, 2008 1:42 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
double quoute can be check by /""
Left by Shafiq Ali on Apr 13, 2008 7:10 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Help, I am using the WordPress plugin: Redirection

and on the website for it it has:

/blog/(.*) => /$1

This will match any URL that starts with /blog/, and will redirect it to the same URL but without /blog/. For example, /blog/2006/10/01/mypost will be redirected to /2006/10/01/mypost.

----
I want to redirect all my post urls that were domain/blog/postname to domain/postname and so I tried to use /blog/(.*) => /$1 but I cannot seem to get it to work. Is the code right? Or maybe the plugin doesn't work with WordPress2.5?
Left by Joni on May 10, 2008 9:56 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I am writing following regular expresion which allows alphabets ,numbers as well special chars .But when I write & i a getting error.

[a-zA-Z0-9][&\_\.!#\$%\*\/\?\^\{\}\+\-=~`]+
Left by Anju Kundwani on Jul 29, 2008 7:08 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
hi i want to validate the decimal like after the integer the decimal shoulb be between 1 to 5


for examaple : 0.2,1.2,1.5,3.2, etccc
the decimal should be between 1 to 5 after the integer
Left by srinivas on Jul 31, 2008 2:46 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
im trying to write regular expression for Url, i write this

[a-zA-Z0-9-_\\$]+(//.[a-za-z0-9-_\/\/$]+)?\\??[a-zA-Z0-9-_\\$]+=?[a-zA-Z0-9-_\\$]+(&[a-zA-Z0-9-_\\$]+=[a-zA-Z0-9-_\\$]+)*";">\\.[a-zA-Z0-9-_\\$]+)?\\??[a-zA-Z0-9-_\\$]+=?[a-zA-Z0-9-_\\$]+(&[a-zA-Z0-9-_\\$]+=[a-zA-Z0-9-_\\$]+)*

but there is an error shown for invalid character, plz help me for write proper reg. expression for validate url
Left by aliasgher on Sep 06, 2008 12:33 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
excellent stuff
Left by jay on Sep 18, 2008 8:53 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hello
I urgently require a regular expression for writing names in the text field.The name can contain white space like ex Abu dhabi or New York and it can even be 1 single word like london or mumbai. it can contain some characters like - ' . in it.How to write a regular expression for this.Please help.thanks in advance.
Humayun Irshad
Left by Humayun Irshad on Sep 24, 2008 11:54 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,
Please help,
i need to import an Excel file.
When selecting a file for import, check the selected file is Excel File.
i got it with [a-zA-Z0_9].*\bxls\b - This ok for Excel File with small letters(eg:ABC.xls or abc.xls)

But, if we are selecting a ABC.XLS or abc.XLS, it will not be help ful.

How i can solve this?
Please let me know ASP
Thanks
Jais
Left by Jaison on Nov 07, 2008 10:00 AM

# regular expresion for email validation with name attached with email id
Requesting Gravatar...
i m searching for email validation where we attach name also for address field
Left by bhumika on Dec 23, 2008 4:33 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Great tutorial. Thank you. How can I write a regex to remove binary characters from a file? Any ideas !!!
Left by Linux-Lover on Dec 25, 2008 12:09 AM

# To learn regular expressions, I recommend biterscripting to my students
Requesting Gravatar...

Hi all:

I am an IT trainer/consultant, and train new developers on regular expressions as part of script programming/scripting. On Windows, I recommend biterscripting to my students. It provides various REs and stream editors to help one get started quickly. Also, it is free to download ( http://www.biterscripting.com ) so sudents can download it on their home computers and play with regular expressions. biterscripting works without any compilers, etc., so the students don't have to download anything else.

Thought I post this for general audience looking to learn regular expressions.

Sen
Left by Sen Hu on Dec 28, 2008 10:40 AM

# Regular Expression
Requesting Gravatar...
These are explained in the best way.. can you give me the complete tutorials.
You can contact me on niazi587 @ gmail.com
Left by Data Entry on Jan 16, 2009 12:11 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Excellent Article
Left by Akram Khan on Feb 25, 2009 3:02 AM

# Put [] to string
Requesting Gravatar...
I have scripts with patten .[abc].tablename and table names are different. I want to change to .[abc].[tablename]. I replace : (\.[abc\]\.)([^\[]\S+)
with:$1[$2]. it does not work. Can somebody help me?
Thanks
Left by lisa on Mar 30, 2009 10:47 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
hi!! i badly need help.. can someone pls tell me how to write regular expressions to allow only .xls files.. thanks
Left by Avi on Apr 10, 2009 6:35 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I want to extract a 10 digit number always starting with number 9.
Can any one help me for this.
I tried 9######### but it did not worked.
please mail the solution to swati.agrawal84@gmail.com
Regards,
Swati.
Left by Swati on Jun 14, 2009 12:38 AM

# Extracting a 10 digit number always starting with 9
Requesting Gravatar...

Using biterscripting ( http://www.biterscripting.com ), to extract a 10 digit number always starting with a 9, you would use the following command.

stex -r "^9(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)^"

stex means string extractor, -r means regular expression, () means one char from a char set, 0>9 means any chars from o through 9.

Say, you want to extract the first number from a file X, use the following.


var str data ; cat "X" > $data
stex -r "^9(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)^" $data



To extract the 3rd number in that file, use the following (notice the 3 after the > ).


var str data ; cat "X" > $data
stex -r "^9(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)^3" $data


To extract everything upto the 3rd number, use the following (notice the ] after then 3).


var str data ; cat "X" > $data
stex -r "^9(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)(0>9)^3]" $data


etc.


Experiment, experiment, experiment. There is always a way to extract anything you want.


Sen



Left by Sen Hu on Jun 30, 2009 10:05 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Thanks.

I have to write a regular expression for followint String.


public static void getIt()
{
String str = "\"portland ,or\", \"James, Fannon\", 1235, \"ST, WS\", CD";

String []temp = str.split("^\"\\w\"$");
for(int i=0;i < temp.length; i++)
System.out.println( i + "--> " + temp[i]);

}

i want result like porland, or
James, Fannor
1235
ST,WS
CD

Thanks
Left by Peeyush on Jul 07, 2009 4:03 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I need to write a regular expression for the string "[[hardik]]".

Please provide help on this.

Thanks in advance
Left by Hardik Raval on Jul 14, 2009 5:16 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Thanks for an elucidative article
Left by squamon on Aug 14, 2009 8:16 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I want to write a pattern based rule to extract only URLs on a webpage that end with ".html". What would I write to achieve this? Thanks.
Left by Randy Macdonald on Oct 25, 2009 10:50 AM

# Help me for this problem
Requesting Gravatar...
In my program I've assigned as follows
name=/^[a-zA-Z -/]+$;
It shows an error for phonenumber and district code
plz anybody tell me the solutions immediatly.
Left by varsh on Oct 30, 2009 7:19 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
superb article !!!

I am trying to validate an URL

"http://www.google.co.in/search?hl=en&client=firefox-a&channel=s&rls=org.mozilla:en-US:official&q=solution+for+invalid+preceding+regular+expression+%2B+c&start=10&sa=N"

using the following regex in c language.

"(http|rtsp)://([^/]+)(/[^\?#]+)*(\?[^#]+)?(#[.]+)?"

But it does not work. How to solve this?

Pls someone help me. its really urgent...
Left by Lakshmanakumar on Dec 07, 2009 12:24 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hello,

Can someone please enlightenme as to: How do I get a regexp to match the second line of the following, but not the first (based on the fact that "Exception caught' is present)?

2009-11-09 14:44:34,689 [11] ERROR raw.showdata [(null)] - Exception caught
2009-11-09 14:45:34,789 [12] ERROR raw.nodata [(null)] - Some other problem

I want all errors to be matched, except ones that say "Exception caught" towards the end of the line. Please help!

Left by WTF on Dec 15, 2009 5:40 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I would like to redirect all urls, using regex, that contains three or more hyphens ('-'). How should I go about it?
Left by Sriraj on Dec 28, 2009 2:18 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Excellent article raju
Left by Gousepash on Jan 19, 2010 1:37 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
BUT... where, oh where does one actually write the expression to make a change take place???
Forgive me if this sounds trite, but it's never been answered for me.
It's something that may be obvious to all of you, but it's never been
so for me.
Left by TTK on Jan 22, 2010 2:22 PM

# lic of india
Requesting Gravatar...
I am Rhea S. I have visited your website and I would like to congratulate you on building such a valuable online resource. I am sure your visitors find your site as useful as I did.

Have a great day.

Thanks and regards,

Rhea S.
Left by Rhea S. on Mar 22, 2010 7:29 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi, i have the expression as the below
((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
to allow users for the phone nos format as 999-999-9999. Its working fine
but also allow users who type as (999)999-9999.
Can some show me how to block users from entering the phone nos with brackets like (999)999-9999 and allow users only with this format 999-999-9999.

Thanks
Left by Peter on Apr 02, 2010 6:22 PM

# LIC
Requesting Gravatar...
Valuable!!!! Thanks!
Left by Elin on Sep 07, 2010 1:58 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Can you provide me with complete details, pleasee?
Left by Penny Lic on Sep 20, 2010 9:20 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
can i get the regular expression for one or more phone numbers seperated by ',' and one phone number is must others are optional and a phone number have minimum 10 digits and maximum 13 digits
Left by sabitha on Dec 14, 2010 11:30 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I need validation similar like declaration of variables or to accept digits along with character and not digits alone,eg:- 3G, Mp3
Left by Sachida on Feb 12, 2011 12:26 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
i need to explain the below one , and i want to to separate digits by (,)comma to ensure that it is a cost how can i do this .... please tell me

<asp:RegularExpressionValidator ID="regexpNameTbEstimatedCost" runat="server" ForeColor="Red" Display="Dynamic"
ErrorMessage="*"
ControlToValidate="tbCost"
ValidationExpression="^(-?\$?([1-9]\d{0,2}(,\d{3})*|[1-9]\d*|0|)(.\d{1,2})?|\(\$?([1-9]\d{0,2}(,\d{3})*|[1-9]\d*|0|)(.\d{1,2})?\))$" />
Left by krishna on Mar 03, 2011 10:46 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,
I am trying to write a regex which will help me retrieve text in between angular brackets in a given text. < String > I want to retrieve string.
I am iterating through the line of the text and trying to use the regex <.*?> but it is not working
Could you please help?
Left by Sayali on Mar 26, 2011 7:20 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,
I'm need of write regex to given text

1999|2002|2006|2008 or more years

plz help me for regex above case.

regards
Left by Basel on Mar 30, 2011 3:47 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
very much help full not no help on strings.
Actually i want to develop a regex that will check that either the entered string contains ".com". Plz help
Left by M Junaid on May 17, 2011 10:40 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hi,

this is really interesting & I've tried to write some expressions myself.
Pls how can I wirite an expression to match the following set of numbers?

08023180128
07029654666
08163514717
8039135443
0802-212 6868

thanks in advance
Left by Folayemi .A on May 19, 2011 2:10 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Nice topic, its very usefull. thanks to you for giving such a document
Left by ganesh on Oct 13, 2011 1:34 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
hi...... kindly some1 tell me that how can i write regular expression for a language of all those strings which do not contain double letter or aa.
Left by jawad ali on Feb 13, 2012 1:00 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Hello. How can I write regular expression for all strings that include both whole numbers and any whitespace whether leading, middle, trailing?
Left by skritch d on Mar 04, 2012 3:40 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Regarding my post a few minutes ago:
<Hello. How can I write regular expression for all strings that include both whole numbers and any whitespace whether leading, middle, trailing?>

I did a test match on the regular expression at http://www.regular-expressions.info/javascriptexample.html equal to: "^[0-9\s]*$"

This is what I needed. However, I receive an "Unrecognized escape sequence" error. It works fine when I use "^[0-9]*$" without the \s, but with it no luck. Not sure why, but I have enough info now. Thanks.
Left by skritch d on Mar 04, 2012 4:06 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
I created regular expression which allow only string in sql server that is:- ^[a-zA-z]*$, then how can i use it in sql server or this expression is right or not.
Left by jyoti on Mar 15, 2012 2:02 AM

# Remove NAN from results
Requesting Gravatar...
could some help me with my formula? I need my results to be 0 instead of NaN

=sum(Fields!StartsSpreadAmt.Value) / sum (Fields!StartsAmt.Value)
Left by Tiffany on Jun 12, 2012 9:59 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Please any one can help me ,
I want to write regular expression in such way ,
textbox should comtain A-Za-z0-9\/()and only one - in that .
plese help me .
Left by roma on Jun 12, 2012 4:19 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
<td align="right"><div class="lastBid"> &nbsp;9999929.00<div></td>

need to extract 9999929 value from this expression. can u tell me the expression? thanks
Left by raja on Jul 17, 2012 12:10 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
it is hard to find a good starting point voor regex. This is one of the better one's! Thanks.

Furthermore:
The title of this blog is: Learn how to write!
If you have any code to debug just go to http://stackoverflow.com/
They will certainly help you, but under the condition you have tried it by yourself first:)

Left by Peter on Aug 16, 2012 4:03 PM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Can someone solve this for me?
Write a regular expression that defines the language{w E {a,b}* :lwl is even}
E = belongs
Left by ben adji on Nov 14, 2012 3:56 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Blogs are the best way to write and show the websites to the readers.
Left by bielat90 on Jan 24, 2013 1:06 AM

# re: Learn how to write a Regular Expression:
Requesting Gravatar...
Blogging are written in many languages as per your understanding you read whatever you want to read from the blog.
Left by New Jersey Nightclubs for sale on Jan 24, 2013 1:10 AM

Your comment:
 (will show your gravatar)


Copyright © Raju | Powered by: GeeksWithBlogs.net | Join free