ShowUsYour<Blog>

Irregular expressions regularly

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
Posted: Sep 23 2003, 06:40 PM by digory | with 11 comment(s)
Filed under:

Comments

TrackBack said:

# September 23, 2003 4:42 AM

OmegaSupreme said:

Thanks for the info, very useful :D
# September 23, 2003 7:21 AM

Bambuk said:

Nice :)
# November 19, 2003 8:04 AM

TrackBack said:

# March 13, 2004 3:21 PM

TrackBack said:

# March 17, 2004 9:05 PM

TrackBack said:

# July 10, 2004 10:48 AM

TrackBack said:

# July 10, 2004 10:56 AM

TrackBack said:

# July 28, 2004 10:15 PM

Katty Flouee said:

It was certainly interesting for me to read this article. Thanx for it. I like such topics and everything that is connected to them. I would like to read more on this site soon. BTW, pretty good design that site has, but what  do you think about changing it from time to time?

Katty  Flouee

<a href="www.baccaratgirls.com/">escorts London 24 7</a>

# September 13, 2010 4:17 AM

Katherine Kripke said:

Interesting article as for me. I'd like to read something more concerning that theme. The only thing this blog needs is some photos of any gizmos.

Katherine Kripke

<a href="www.jammer-store.com/">cell blocker</a>

# September 20, 2010 11:07 AM

Whitny Trider said:

Wow, truly nice post. Where can I get this RSS?

Whitny  Trider

<a href="www.wirelesscameradetectors.com/">rf detection</a>

# November 18, 2010 2:04 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)