Click here to Skip to main content
15,885,278 members
Articles / Web Development / ASP.NET
Article

Understanding Regular Expressions in .NET

Rate me:
Please Sign up or sign in to vote.
1.00/5 (7 votes)
4 Nov 20021 min read 75.5K   15   7
I've created a Regex evaluator. It has proven to be extremely helpful. Please feel free to use, and e-mail me if you want the source.

The evaluator can be found here: RegexEvaluate.aspx

The evaluator was a learning process in itself, but well worth it. You'll want to do a little research in the SDK on .NET regular expression syntax. I needed the evaluator to help creat parsing expressions for SQL and HTML. Generating the correct expression would have been almost impossible without this tool to test with. I don't know how much people get into using regular expressions, but they are incredibly useful in a myriad of situations.

Pay attention to using grouping syntax like (?....). Makes a big difference.

The following is direct code I use to parse HTML. I'm a JScript.NET fiend so you'll have to bear with me. I wish I knew a built in .NET way to do this but it hasn't made itself known to me.

class RegularExpressions {

    static function TagOpen(tagname:String) :String
        { return '<\\s*(?<tagname>'+tagname+')\\s*(?<ATTRIBUTES>(?:\\s*\\b\\w+\\b\\s*(?:=\\s*(?:"[^"]*"|\'[^\']*\'|[^"\'<> ]+)\\s*)?)*)/?\\s*>' }
    static function TagClose(tagname:String) :String
        { return '<\\s*/\\s*(?<tagname>'+tagname+')\\s*>' }
    static function NameValue(name:String) :String
        { return '(?<name>'+name+')(\\s*=\\s*("(?<value>[^"]*)"|\'(?<value>[^\']*)\'|(?<value>[^"\'<> ]+)))?' }
    static function MLtags(tagname:String) :Regex
        { return new Regex( TagOpen(tagname)+"|"+TagClose(tagname), RegexOptions.IgnoreCase ) }
    static function MLopentags(tagname:String) :Regex
        { return new Regex( TagOpen(tagname) ) }
    static function NVpair(name:String) :Regex
        { return new Regex( NameValue(name), RegexOptions.IgnoreCase )  }
    static const HTMLtags:Regex = MLtags('\\w+')
    static const IMGtags:Regex = MLopentags('IMG')
    static const NameValuePairs:Regex = NVpair('\\w+')
    static const Email:Regex = new Regex( '(?:\w+[.]?)+@\w+(?:[.]\w+)+', RegexOptions.IgnoreCase)

}
Sorry to leave this one as a puzzle, but you should be able to figure it out if you need it.

The SQL expressions and methods I created are much more complex and I would have a difficult time explaining it to myself now. But I would love for someone to call me an idiot for making these and show me a better way. The HTML parsing was necessary to break html into controls so that certain controls could be replaced with their programmatic counterparts (like an <img> tag). The SQL expressions were created to help in eliminating small differences in SQL statements like capitalization and spacing. And to break down the expression accurately to help in caching data / determining cached data.

I hope this helps...
--Oren

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Just a crazy developer with crazy ideas of grandure.

What's that? Anal probe? NO, I said "Anal CODE!" Get that thing away, and get back to EditPlus.

Comments and Discussions

 
NewsThere's a new version of the RegEx Tester Tool ! Pin
Pablo Osés1-Mar-08 23:27
Pablo Osés1-Mar-08 23:27 
GeneralQuestion about Regex Pin
Mazdak14-Aug-03 6:03
Mazdak14-Aug-03 6:03 
GeneralGreat to Me Pin
KZ13-Nov-02 6:00
KZ13-Nov-02 6:00 
GeneralThanks for sharing but, Pin
Sijin5-Nov-02 18:28
Sijin5-Nov-02 18:28 
GeneralRe: Thanks for sharing but, Pin
essence5-Nov-02 18:56
essence5-Nov-02 18:56 
GeneralRe: Thanks for sharing but, Pin
Sijin6-Nov-02 2:37
Sijin6-Nov-02 2:37 
GeneralRe: Thanks for sharing but, Pin
essence6-Nov-02 16:45
essence6-Nov-02 16:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.