Regex Reminders #0 - Commenting Regular Expressions

Free spacing and comment mode
.NET Regular Expressions allow you to embed comments and whitespace within pattern strings making them clearer to 
read (because of the whitespace), and easier to maintain (due to the modularity). To use this feature you must turn on the RegexOptions.IgnorePatternWhitespace flag. This can be achieved
either by using the RegexOptions enumerated datatype or, via the (?x) mode modifier. Therefore the following
two examples are functionally identical: Regex re = new Regex( @" # This pattern matches Foo (?i) # turn on insensitivity # The Foo bit \b(Foo)\b " , RegexOptions.IgnorePatternWhitespace ) ; Regex re = new Regex( @"(?x) # This pattern matches Foo (?i) # turn on insensitivity # The Foo bit \b(Foo)\b " ) ; Using IgnorePatternWhitespace option, comments can be embedded in one of two ways. Firstly, a raw '#'
character can be used to mark the beginning of a comment and the comment runs up to the first NEWLINE
(\n) character that is encountered. This can be easily achieved in C# with verbatim strings @"..."
or in VB by appending newline characters within the string using Chr(10), vbCrLf or Environment.NewLine.
Comments can also be declared using the (?#...) syntax. The following 3 snippets demonstrate examples of each of these methods... C# Verbatim Strings. using System; using System.Collections; using System.Text.RegularExpressions ; namespace Regex Snippets.Tests { public class CommentedCSharp { public static void Main() { Regex re = new Regex( @" # This pattern matches Foo (?i) # turn on insensitivity # The Foo bit \b(Foo)\b " , RegexOptions.IgnorePatternWhitespace ) ; for ( Match m = re.Match( "foo bar Foo" ) ; m.Success ; m = m.NextMatch() ) Console.WriteLine( m.Value + Environment.NewLine ) ; Console.ReadLine(); } } } VB.NET using (?#...) syntax. Option Strict On Imports System.Text.RegularExpressions Namespace Regex Snippets.Tests Module CommentedVBOne Public Sub Main() ' Demonstrates a VB Whitespace pattern using the '" (?#...) " & _ ' syntax Dim re As New Regex( _ " (?# This pattern matches Foo ) " & _ " (?i) (?# turn on insensitivity ) " & _ " (?# The Foo bit ) " & _ " \b(Foo)\b " _ , RegexOptions.IgnorePatternWhitespace) Dim m As Match = re.Match("foo bar foo") Dim s As String = "" While m.Success s &= (m.Value & Environment.NewLine) m = m.NextMatch() End While MessageBox.Show(s) End Sub End Module End Namespace VB.NET using appended newlines. Option Strict On Imports System.Text.RegularExpressions Namespace Regex Snippets.Tests Module CommentedVBTwo Public Sub Main() ' Demonstrates a VB Whitespace pattern using the '" (?#...) " & _ ' syntax Dim re As New Regex( _ " # This pattern matches Foo " & vbCrLf & _ " (?i) # turn on insensitivity " & vbCrLf & _ " # The Foo bit " & vbCrLf & _ " \b(Foo)\b " _ , RegexOptions.IgnorePatternWhitespace) Dim m As Match = re.Match("foo bar foo") Dim s As String = "" While m.Success s &= (m.Value & Environment.NewLine) m = m.NextMatch() End While MessageBox.Show(s) End Sub End Module End Namespace

10 Comments

  • Thanks for the info, very useful :D

  • Kx7eUQ Very neat blog.Really looking forward to read more. Great.

  • I stopped counting them long ago, but I would guess that there are somewhere around
    4 - 5,000 volumes of one kind or another, both hard covers and paperbacks
    in my home. Low tech message boards will never go out of style
    and for good reason. Check out these comparisons,
    and why one might prove more satisfactory for you than
    another.

  • Hello, I desire to subscribe for this blog to get latest updates, thus where can i do it please assist.

  • Find all of your favorite designs and designers only at
    Lollipop - Moon. One variation is the game called White Elephant.
    On Black Friday she found a much better set that was on sale that morning for only $80.

  • Gum. As with most other out of doors Christmas decorations,.
    Utilize great tiongkok along with cups instead of disposable discs, cups, and also plastic material utensils.

  • Seeking to replace the ole John Mc - Cain who ran us into this ditch a couple of years ago.
    ) relic. The oranges aren't really suited to eating - it's more
    like you are drinking fruit than eating it, way too juicy.

  • Can I simply say what a relief to discover an individual
    who truly knows what they're talking about on the internet. You definitely realize how to bring a problem to light and make it important. More people have to read this and understand this side of the story. I can't
    believe you are not more popular given that you definitely possess the gift.

  • President, I think you’ll agree, this would probably
    put an end to all the name-calling that Democrats are
    currently subjected to thanks to our donkey symbol. Pets as
    presents seem like a great idea, but the logistics of owning a pet came
    be very tricky. Three, have some fun while searching.

  • Have you ever thought about adding a little bit more than just your articles?
    I mean, what you say is important and everything.
    Nevertheless imagine if you added some great graphics or
    video clips to give your posts more, "pop"! Your content is
    excellent but with images and video clips, this blog
    could undeniably be one of the greatest in its field.
    Good blog!

Comments have been disabled for this content.