Sunday, January 17, 2010

Formatting and documenting your regex

Regular expressions are a very powerful tool for pattern matching, but a complicated
regex can be very difficult for a human to read and to comprehend. That is why,
like any good code, a good regular expression must be well formatted and documented.

Here are some guidelines when formatting and documenting your regex:

1. Keep each line under 80 characters, horizontal scrolling reduces readability.
2. Break long patterns into multiple lines, usually after a space or a line break.
3. Indent bracers to help think in the right scope.
4. Format complicated OR patterns into multiple blocks like a case statement.
5. Comment your regex on what it does, don't just translate it into English.

# Match <BODY
# Match any non > char for zero to infinite number of times
Bad example: Comment that translates the regex into English.

# Match the BODY tag
# Match any character in the body tag
# Match the end BODY tag

Good example: Comment that explains the purpose of the pattern.

((System\.)?Drawing\.)?ContentAlignment\.(?! TopLeftMiddleLeftTopCenterMiddleCenter)\w*)(?!(?<=\k<Name>\.Image.*)(?

Bad Example: Pray you never have to modify this regex.


# Match for Label or TextBox control

# Store name into <name> group


# Match any non-standard TextAlign

# Store any match in Result group for error reporting in CA


# Match for control's TextAlign Property


# Match for possible namespace


# Match any ContentAlignment that is not in the group



# Skip any Control that has image on it
Good Example: Now it make sense!