Monday, December 05, 2005

Regular Expressions in .Net

Regular Expressions

Regular expressions have been widely popular in languages such as PERL and AWK and have been utilized for pattern matching, text manipulation and text searching. These languages are specifically is known for its advanced pattern matching features. Dot Net regular expressions are based on that of Perl and are compatible with Perl 5 regular expressions.

To begin with, they are not as complex as they look, especially if you start experimenting with them. I would recommend that you download a tool such as Expresso (http://www.ultrapico.com/), to become familiar with regular expressions.

Regular Expression Elements

Some of the commonly used regular expression elements are:

^

Matches start of input

$

Matches end of input

.

Matches any character except new line

|

OR

*

Match the preceding expression 0 or more number of times

+

Match the preceding expression 1 or more number of times

?

Match the preceding expression 0 or 1 number of times

()

Logical group / sub-expression (capture as auto number group)

(?(exp))

Named capture group

(?=exp)

Match any position preceding a suffix exp

(?<=exp)

Match any position following a prefix exp

(?!exp)

Match any position after which exp is not found

(?

Match any position before which exp is not found

[…]

List of characters to match

[^expression]

Not containing any of the specified character

{n} or {n. m}

Quantifier (Match exact number or range of instances)

(?(exp (yes|no))

If expression (exp) is true match yes part else no part

\

Escape character (to match any of the special characters)

\w

Match any word character

\W

Match any non-word character

\s

Match any white space character

\S

Match any non-white space character

\d

Match any numeric digit

\D

Match any numeric digit

\b

Match a backspace if in character matching mode ([]).

Otherwise match the position at beginning or end of a word

\t

Match tab

\r

Match carriage return

\n

Match line feed


The following are matching substitutions:

num

Substitute last substring matched by group number num

${name}

Substitute last substring matched by group name

$&

Substitute a copy of entire text itself

$`

Substitute all the text of the input string before match

$’

Substitute all the text of the input string after match

$+

Substitute last matched group

$_

Substitute input string

$$

Substitute literal $


Regular expressions could also be used to find repeating patterns by making use of backreferencing, using which you can name a pattern found and then use that reference elsewhere in expression. This naming of patterns is also useful in case we need to parse a string like free form date or time strings.

Some Example Regular Expressions

  • Match a word - \btest\b
  • Match all 6 letter words - \b\w{6}\b
  • Match all 6 digit numbers - \b\d{6}\b
  • Match any number \b\d+\b

Instead of giving loads of examples here, I suggest that you download Expresso and check its analyzer view for detailed analysis of the regular expression.

Regular Expressions in .Net

As already discussed, .Net regular expressions are based on that of Perl and are compatible with Perl 5 regular expressions. Dotnet contains a set of powerful classes that makes it even easier to use regular expressions. The classes are available in the System.Text.RegularExpressions namespace.

How to validate an input string in .Net

  • Create a Regex object ‘RegexObj’
  • Call RegexObj.IsMatch (subjectString ), which will return a Boolean showing validity of input string

How to perform regular expression substitution (search and replace) in .Net

  • Create a Regex object ‘RegexObj’
  • Call RegexObj.Replace ( subjectString, replaceString ), which will return a Boolean showing validity of input string

How to parse an input string in .Net

  • Create a Regex object ‘RegexObj’, make sure to name the expressions
  • Call RegexObj.Match ( subjectString ), which will return a list of matches in the input string as per the match regular expression
  • Iterate through the matches to perform post parsing

Free form time parsing function in DotNet

For an example, I have developed a simple free format time parser. I have provided the code and details in this code project article.

References and Further

51 Comments:

At 12:31 PM, Blogger Candice B. said...

Hi -

While searching for some google people find related blogs, I came across your blog. Very nice, I must say! If you get a chance, please visit my google people find site too!

Thank you

 
At 3:45 AM, Blogger linroc said...

Millennium Plus operates using an innovative hybrid of technologies, including GPS satellites and patented microburst cell technology. Each time you communicate with the transceiver in the vehicle, there is a two way flow of information.

* When accessing Millennium Plus via the Internet using any standard browser, you send out a signal. That signal then travels to the cell phone tower nearest your current location.

* Microburst technology operates on existing cellular networks to send and receive data packets, tapping into unused channels on the back side of standard cell frequencies.

* The GPS satellites then locate and communicate with the Millennium Plus transceiver on board your vehicle, which exchanges information on location and speed, as well as executing any commands you've sent.

* The cell towers then relay the signal back to you via the exact means by which it was sent.

gps systems,google gps,
car gps,gps navigation system,handheld gps,
gps reviews,gps maps,gps navigation,cell phone gps,gps tracking,gps software,gps units,bluetooth gps,

 
At 4:19 PM, Blogger Quickregister Link Exchange said...

Ovais Khan,

I saw your post
regarding web hosting directory .

You are welcome to place a link to
your blog or website on my high
traffic website for free. See:

http://www.thefreeadforum.com


The Free Ad Forum is a forum where you may
post your permanent search engine friendly ads daily for
free. I hope you take advantage of this free advertising
opportunity, We have a special section just for web hosting directory .

Thank you,

John,

http://www.thefreeadforum.com
The Free Advertising Forum.

 
At 8:17 PM, Anonymous Anonymous said...

IS VERY GOOD
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

 
At 9:32 PM, Blogger HildegardWegener said...

good~..................................................

 
At 4:07 AM, Anonymous Anonymous said...

If you can not be kind, at least have the decency to be vague.............................................

 
At 4:08 AM, Anonymous Anonymous said...

快樂的微笑是保持生命康健的唯一藥石,它的價值千萬,卻不要花費一文錢..............................

 
At 4:22 AM, Blogger 正常關西 said...

裸體裸照裸女愛愛無碼尋夢視訊聊天a漫a片a圖一夜情一葉情人妻激情情色寫真美女自拍辣妹自拍正妹自拍美女走光辣妹走光正妹走光脫衣秀脫衣走光色情自慰自拍成人全裸打炮打手槍打飛機巨乳巨奶女優

 
At 8:16 AM, Blogger 無奈 said...

You may be disappointed if you fail, but you are doomed if you do not try...................................................

 
At 8:34 AM, Anonymous Anonymous said...

當身處逆境時,要能忍一時之氣,吃一時之苦,並及早確立自己的目標,總有一天,你會嘗到那甘美的果實。........................................

 
At 9:26 PM, Anonymous Anonymous said...

Unable to give you a heart. so have a reply to push up your post. ........................................

 
At 4:30 AM, Anonymous Anonymous said...

Joy often comes after sorrow, like morning after night.......................................................

 
At 12:16 AM, Anonymous Anonymous said...

從人生中拿走友誼,猶如從生活中移走陽光........................................

 
At 1:09 PM, Anonymous Anonymous said...

ut聊天77p2p85cc85st85街視訊視訊聊天ava片a片下載成人情色色情影音視訊聊天洪爺影城洪爺免費視訊免費a片免費一對多utsogo論壇ut聊天室成人片免費看................

 
At 10:44 AM, Anonymous Anonymous said...

很喜歡你的blog哦...加油唷 ........................................

 
At 4:52 AM, Blogger 林韻如 said...

得意人,聽腳步聲就知道。......................................................

 
At 7:15 AM, Anonymous Anonymous said...

很棒的分享~~~來留個言囉~~~~........................................

 
At 3:22 AM, Anonymous Anonymous said...

Learn wisdom by the follies of others. ......................................................

 
At 3:23 AM, Anonymous Anonymous said...

Learning makes a good man better and ill man worse.......................................................

 
At 5:54 PM, Anonymous Anonymous said...

you two make a lovely couple!............................................................

 
At 12:32 AM, Blogger 王周宏儒 said...

路過--你好嗎..很棒的BLOG...................................................

 
At 8:07 PM, Anonymous Anonymous said...

No pains, no gains...................................................................

 
At 4:35 AM, Anonymous Anonymous said...

要持續更新下去喲!!祝你心情愉快............................................................

 
At 4:36 AM, Anonymous Anonymous said...

文章不求沽名釣譽,率性就是真的..................................................................

 
At 12:51 PM, Blogger 銘木 said...

人因夢想而偉大,要堅持自己的理想哦......................................................................

 
At 8:29 PM, Anonymous Anonymous said...

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」......................................................................

 
At 2:16 PM, Anonymous Anonymous said...

成熟,就是有能力適應生活中的模糊。.................................................................

 
At 2:16 PM, Anonymous Anonymous said...

成熟,就是有能力適應生活中的模糊。.................................................................

 
At 9:16 PM, Anonymous Anonymous said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

 
At 3:54 AM, Anonymous Anonymous said...

嘩做左推介BLOG 果然人氣勁旺..................................................................

 
At 6:12 AM, Anonymous Anonymous said...

到處逛逛~~來繞繞留個言囉~~~~.......................................................

 
At 11:07 AM, Anonymous Anonymous said...

Subtlety is better than force. ............................................................

 
At 11:07 AM, Anonymous Anonymous said...

唯有用熱情、用智慧去觀察事物,這事物才會把他的秘密,洩漏給我們............................................................

 
At 11:06 PM, Anonymous Anonymous said...

Practice what you preach.............................................................

 
At 2:30 PM, Anonymous Anonymous said...

One swallow does not make a summer...................................................

 
At 10:20 PM, Anonymous Anonymous said...

不妄求,則心安;不妄作,則身安!.......................................................

 
At 4:17 PM, Anonymous Anonymous said...

天氣涼了~要注意身體喔@~@............................................................

 
At 4:17 PM, Anonymous Anonymous said...

Man proposes, God disposes..................................................................

 
At 4:17 PM, Anonymous Anonymous said...

Subtlety is better than force. ............................................................

 
At 6:36 AM, Anonymous Anonymous said...

Learning makes life sweet.

 
At 9:04 AM, Anonymous Anonymous said...

成熟,就是有能力適應生活中的模糊。............................................................

 
At 6:36 PM, Anonymous Anonymous said...

精彩的部落格 要繼續加油 ..................................................................

 
At 3:44 PM, Blogger 建邱勳 said...

幸福不是一切,人還有責任。............................................................

 
At 3:26 AM, Anonymous Anonymous said...

知足常樂~~有這麼好的文章,人生足矣~~哈哈............................................................

 
At 8:29 PM, Anonymous Anonymous said...

Poverty is stranger to industry.........................................

 
At 7:15 AM, Blogger 孫邦柔 said...

文章不求沽名釣譽,率性就是真的..................................................................

 
At 3:19 PM, Blogger 江仁趙雲虹昆 said...

不要去想沒拿到的東西,多想想自己手裡所擁有的..................................................

 
At 6:59 PM, Anonymous Anonymous said...

Quality is better than quantity...................................................................

 
At 12:55 PM, Anonymous Anonymous said...

you got it!i like it!!!

 
At 4:44 PM, Anonymous Anonymous said...

困難的不在於新概念,而在於逃避舊有的概念。......................................................................

 
At 10:36 PM, Blogger wdmtest said...

Asp.Net Tutorial
Introducing Web Portals and Dropthings.com
Defining a Web Portal
Defining a Web 2.0 Portal
Using a Web Portal
Navigating Dropthings
Using ASP.NET AJAX
Using C# 3.0 and .NET 3.5
Architecting the Web Portal and Widgets
Using a Widget Framework
Adding Widgets
Maximizing the First-Visit Experience
Rendering a Second-Visit Experience
Improving ASP.NET AJAX Performance
Adding Authentication and Authorization
Preventing Denial-of-Service Attacks
Building the Web Layer Using ASP.NET AJAX
Implementing the Start Page of a Web Portal
Building a Custom Drag-and-Drop Extender for a Multicolumn Drop Zone
Implementing WidgetContainer

 

Post a Comment

<< Home