posts - 50, comments - 102, trackbacks - 170

My Links

News

Article Categories

Archives

Post Categories

Image Galleries

Friends Blog

Learn how to write a Regular Expression:

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 :))

Print | posted on Thursday, October 23, 2003 10:39 AM |

Feedback

Gravatar

# re: Learn how to write a Regular Expression:

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
10/25/2003 7:45 AM | Tameem Ansari
Gravatar

# re: Learn how to write a Regular Expression:

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.
10/27/2003 6:33 AM | Ramchander
Gravatar

# re: Learn how to write a Regular Expression:

DotNetNuke 2.2
10/5/2004 3:59 AM | BangTech, Inc.
Gravatar

# re: Learn how to write a Regular Expression:

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!
3/11/2005 12:28 AM | Nghia Ngo
Gravatar

# re: Learn how to write a Regular Expression:

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


3/11/2005 8:55 AM | Ramchander
Gravatar

# re: Learn how to write a Regular Expression:

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
5/10/2005 8:19 PM | Ruslan
Gravatar

# re: Learn how to write a Regular Expression:

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...
5/16/2005 2:51 PM | Dan
Gravatar

# re: Learn how to write a Regular Expression:

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
5/26/2005 12:31 AM | Nirmal
Gravatar

# re: Learn how to write a Regular Expression:

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+\"");

6/21/2005 1:21 AM | Ali
Gravatar

# re: Learn how to write a Regular Expression:

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;
}
6/23/2005 4:57 PM | Shane
Gravatar

# re: Learn how to write a Regular Expression:

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
6/28/2005 3:17 PM | george
Gravatar

# Learn how to write a Regular Expression:

TrackBack From:http://cy163.cnblogs.com/archive/2005/07/29/202659.html
7/28/2005 8:58 PM | cy163
Gravatar

# re: Learn how to write a Regular Expression:

Hi,
I need val exp that the number should be >0

0.0 invalid
0 is invalid

0.01 valid ...

1 valid ...

Thanks
Jay
7/30/2005 8:40 AM | Jay Dubal
Gravatar

# re: Learn how to write a Regular Expression:

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
10/18/2005 10:56 AM | Vikram
Gravatar

# re: Learn how to write a Regular Expression:

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
10/27/2005 8:15 AM | Dheeraj Kumar
Gravatar

# re: Learn how to write a Regular Expression:

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.
11/29/2005 12:41 AM | Priya
Gravatar

# re: Learn how to write a Regular Expression:

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
1/6/2006 11:37 PM | Kamal Ramesh
Gravatar

# re: Learn how to write a Regular Expression:

Hi guys, am tryin to write an If Statement for the new Visual studio express. Pls how can i do it.
1/27/2006 2:35 PM | Adams Olu
Gravatar

# re: Learn how to write a Regular Expression:

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
2/15/2006 4:31 PM | Ravish Jain
Gravatar

# Regular Expression for number both integers and with decimal

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
2/15/2006 4:35 PM | Ravish Jain
Gravatar

# re: Learn how to write a Regular Expression:

I want the regular expresssion code for using in htaccess file to redirect to somefile.html
4/7/2006 4:12 PM | vikas
Gravatar

# re: Learn how to write a Regular Expression:

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
4/13/2006 11:47 PM | Aditya N H
Gravatar

# re: Learn how to write a Regular Expression:

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$
5/22/2006 3:57 PM | Meghana Randad
Gravatar

# re: Learn how to write a Regular Expression:

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!
3/4/2007 8:15 PM | Frank Drebin
Gravatar

# re: Learn how to write a Regular Expression:

Hi,

I want Create New Extraction Rules Type for Pattern Based Rule

for (xxx) xxxx-xxxx-xxxx

how to setting pls ans


thk
elaine
5/7/2007 1:56 AM | Elaine Hui
Gravatar

# re: Learn how to write a Regular Expression:

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
6/26/2007 10:14 AM | Syed Saleem
Gravatar

# re: Learn how to write a Regular Expression:

not a useful link.
7/5/2007 3:56 AM | testuser
Gravatar

# re: Learn how to write a Regular Expression:

hi see this regulare expression topics
8/24/2007 4:53 AM | Gurunathan
Gravatar

# re: Learn how to write a Regular Expression:

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
9/26/2007 7:39 PM | olu
Gravatar

# re: Learn how to write a Regular Expression:

Very useful.Great work.

Thanx n rgds
Prince D
10/8/2007 10:05 AM | prince
Gravatar

# re: Learn how to write a Regular Expression:

This is a wonderful resource! I am very happy to have found it. It already helped me with a big issue at work. THANKS!!!
11/26/2007 2:10 PM | Jason Green
Gravatar

# re: Learn how to write a Regular Expression:

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.
'
11/27/2007 8:41 AM | girish
Gravatar

# re: Learn how to write a Regular Expression:

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;
12/20/2007 12:15 AM | Mayank Parmar
Gravatar

# re: Learn how to write a Regular Expression:

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.
12/26/2007 3:23 AM | tanu
Gravatar

# re: Learn how to write a Regular Expression:

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
2/7/2008 10:40 AM | Steven Justice
Gravatar

# re: Learn how to write a Regular Expression:

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.
2/26/2008 5:56 PM | 1up
Gravatar

# re: Learn how to write a Regular Expression:

My bad, it works. The double quote had been converted to an html entity.
2/26/2008 6:03 PM | 1up
Gravatar

# re: Learn how to write a Regular Expression:

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!

3/6/2008 9:37 AM | dash
Gravatar

# re: Learn how to write a Regular Expression:

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.
3/6/2008 11:57 AM | Jim
Gravatar

# re: Learn how to write a Regular Expression:

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?
3/16/2008 4:11 AM | universe
Gravatar

# re: Learn how to write a Regular Expression:

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
3/28/2008 1:42 AM | Michael
Gravatar

# re: Learn how to write a Regular Expression:

double quoute can be check by /""
4/13/2008 7:10 AM | Shafiq Ali
Gravatar

# re: Learn how to write a Regular Expression:

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?
5/10/2008 9:56 PM | Joni
Gravatar

# re: Learn how to write a Regular Expression:

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][&\_\.!#\$%\*\/\?\^\{\}\+\-=~`]+
7/29/2008 7:08 AM | Anju Kundwani
Gravatar

# re: Learn how to write a Regular Expression:

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
7/31/2008 2:46 AM | srinivas
Gravatar

# re: Learn how to write a Regular Expression:

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
9/6/2008 12:33 AM | aliasgher
Gravatar

# re: Learn how to write a Regular Expression:

excellent stuff
9/18/2008 8:53 AM | jay
Gravatar

# re: Learn how to write a Regular Expression:

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
9/24/2008 11:54 PM | Humayun Irshad
Gravatar

# re: Learn how to write a Regular Expression:

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
11/7/2008 10:00 AM | Jaison
Gravatar

# regular expresion for email validation with name attached with email id

i m searching for email validation where we attach name also for address field
12/23/2008 4:33 AM | bhumika
Gravatar

# re: Learn how to write a Regular Expression:

Great tutorial. Thank you. How can I write a regex to remove binary characters from a file? Any ideas !!!
12/25/2008 12:09 AM | Linux-Lover
Gravatar

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


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
12/28/2008 10:40 AM | Sen Hu
Gravatar

# Regular Expression

These are explained in the best way.. can you give me the complete tutorials.
You can contact me on niazi587 @ gmail.com
1/16/2009 12:11 AM | Data Entry
Gravatar

# re: Learn how to write a Regular Expression:

Excellent Article
2/25/2009 3:02 AM | Akram Khan
Gravatar

# Cheap tickets

Badly need your help. Keep cool and you command everybody. Help me! I find sites on the topic: In the airline quality rating report.. I found only this - [url="http://cheapest-tickets.us/"]Airline Tickets[/url]. Find local businesses and services by name and location. Bosnia, airline tickets to eastern europe, call milica obradovic airline tickets to serbia, airline tickets to croatia, airline tickets to. Waiting for a reply ;-), Hasna from Kazakhstan.
3/23/2009 3:44 PM | Hasna
Gravatar

# Put [] to string

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
3/30/2009 10:47 AM | lisa
Gravatar

# re: Learn how to write a Regular Expression:

hi!! i badly need help.. can someone pls tell me how to write regular expressions to allow only .xls files.. thanks
4/10/2009 6:35 AM | Avi
Gravatar

# re: Learn how to write a Regular Expression:

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.
6/14/2009 12:38 AM | Swati
Gravatar

# Extracting a 10 digit number always starting with 9


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



6/30/2009 10:05 AM | Sen Hu
Gravatar

# re: Learn how to write a Regular Expression:

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
7/7/2009 4:03 AM | Peeyush
Gravatar

# re: Learn how to write a Regular Expression:

I need to write a regular expression for the string "[[hardik]]".

Please provide help on this.

Thanks in advance
7/14/2009 5:16 AM | Hardik Raval
Gravatar

# re: Learn how to write a Regular Expression:

Thanks for an elucidative article
8/14/2009 8:16 AM | squamon
Gravatar

# re: Learn how to write a Regular Expression:

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.
10/25/2009 10:50 AM | Randy Macdonald
Gravatar

# Help me for this problem

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.
10/30/2009 7:19 AM | varsh
Gravatar

# re: Learn how to write a Regular Expression:

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...
12/7/2009 12:24 AM | Lakshmanakumar
Gravatar

# re: Learn how to write a Regular Expression:

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!

12/15/2009 5:40 PM | WTF
Gravatar

# re: Learn how to write a Regular Expression:

I would like to redirect all urls, using regex, that contains three or more hyphens ('-'). How should I go about it?
12/28/2009 2:18 AM | Sriraj
Gravatar

# re: Learn how to write a Regular Expression:

Excellent article raju
1/19/2010 1:37 AM | Gousepash
Gravatar

# re: Learn how to write a Regular Expression:

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.
1/22/2010 2:22 PM | TTK
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: