Attention: We are retiring the ASP.NET Community Blogs. Learn more >

A new Regular Expression article in the wind

I actually started putting pen-to-paper tonight for a new Regular Expression article that I've been meaning to write for some time.  The goals of the article are to provide an explanation of each of the following items:

  • Named Captures
  • Positive and Negative lookarounds
  • RightToLeft
  • Multi-line, commented RegEx's
  • Explain the relationship of Matches and Groups
  • Conditional matching

If anybody has an excellent business case example of using RightToLeft semantics I'd love to hear about it; please drop me a line. [ View|Add Comments ]

Obviously much of my learning regarding Regular Expressions has been guided by the writing of Jeffrey Friedl.  For those who haven't read this classic article, I would highly commend it to you:

Understanding Regular Expressions

Or, of course, you could buy the book ;-)

 Mastering Regular Expressions (2nd edition)
 Jeff Freidl's RegEx book!

Cya...

Precedents deliberately established by wise men are entitled to great weight. - Henry Clay

2 Comments

  • One good use for RegexOptions.RightToLeft is converting a flat index into a text file into human-manageable line/column numbers.





    Let's say you're parsing a text file -- with Regexes, of course, so you're representing the whole file as a single string in memory.





    Now, let's say you find a problem. Where is it? The user is not going to be very happy if you tell him it's at character offset 54872. He's going to want to see a line number (at least) and hopefully a column number.





    Now, there are many ways to skin this cat... certainly some are more performant than others. But for a quick and dirty solution, you can't get much simpler than using an RTL regex!





    Here you go... if you use this in your book, can I get a shout-out in the liner notes? :)


    -S








    Regex lineStartRTL = new Regex( @"^",


    RegexOptions.Multiline|RegexOptions.RightToLeft);





    private int IndexToLineNumber( int index)


    {


    if ( index >= this.source.Length)


    index = this.source.Length-1;





    // Line numbers are conventionally one-based, so


    // return the count of line-starts to our "left"


    return this.lineStartRTL.Matches(


    this.source, index).Count;


    }





    private int IndexToColumnNumber( int index)


    {


    if ( index >= this.source.Length)


    index = this.source.Length-1;





    // Column numbers are conventionally zero-based, so


    // return the diff between us and the first char on


    // the line


    int begin = this.lineStartRTL.Match(


    this.source, index).Index;


    return index-begin;


    }


  • Thanks Shawn, love your work! :-)

Comments have been disabled for this content.