You have one or more complex regular expressions that may exist in a file outside of your code. You need a way to place comments within the regular expression itself. These comments will aid others in being able to read and maintain your regular expressions later on.
comments to the regular expression using the
string matchPattern = @"\\\\ # Find this: \\ (?<TheServer>\w*) # Server name \\ # Find this: \ (?<TheService>\w*)\\ # Service name";
or add C#-style comments outside of the regular expression string:
string matchPattern = @"\\\\" + // Find this: \\ @"(?<TheServer>\w*)" + // Server name @"\\" + // Find this: \ @"(?<TheService>\w*)\\"; // Service name
When using these expressions in a
enumeration value must be added to the
parameter of the
Regex object constructor:
Regex RE = new Regex(matchPattern, RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace); MatchCollection theMatches = RE.Matches("The source text goes here...");
With large and complex regular expressions, it is desirable to break up the expression into manageable pieces and to identify what each piece does. For example, the regular expression in the Solution section will pull the server and service pieces out of a UNC string. By breaking up the regular expression onto separate lines and adding comments to each line, we have allowed other developers (who ...