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