Base types, Collections, Diagnostics, IO, RegEx…
Regular Expressions (RegEx) are a powerful tool for searching for text that matches specific patterns, but it is also a complex tool that requires care and attention to detail. There are many caveats to using RegEx. Ron has recently published a few excellent blog posts on the .NET RegEx engine [part 1 | part 2 | part 3]. Here, I would like to briefly talk about a common caveat when using RegEx: the set operator  and its effect on the contained tokens:
A RegEx expression can change its meaning when surrounded by the set operator [ ].
Programmers are used to think in terms of modular expressions. Typically, they assume that an expression does not change its value regardless where you put it. Assume the expression "xyz" evaluates to something specific when it stands by itself:
We are used to assuming that it will evaluate to the same thing when used to provide a value to a function:
or as a component in a larger expression:
(xyz) + 11
However, RegEx fails this intuition. Consider the RegEx set operator . It matches against any character that belongs to the set described in the square brackets. For instance:
Note that in each case, the complete set pattern matches against one character in the input text.
Recall another RegEx construct: the grouped expression ( ). It matches against the pattern contained in the parentheses. Grouped expressions are also used to find specific areas within a larger match. For instance, the RegEx pattern "def(ghi)jkl(m)" matches against the input string 'abcdefghijklmn' as follows:
A program can refer to such expressions by index (see here and here for more information about this).
Expressions can be combined by alteration constructs in the same way as characters can. For instance, the pattern "a|b|c" matches one of the characters 'a', 'b' or 'c', i.e. the same as "[abc]". The pattern "(ay)|(bee)|(see)" matches one of the strings 'ay', 'bee' or 'see'. This may suggest that the latter pattern matches the same as "[(ay)(bee)(see)]". Right?
Let us try to construct a RegEx pattern to match all English articles. In other words we need to match any string from the set that contains the strings 'the', 'a' and 'an' (we ignore capital letters for clarity). We will use the following example to track our success:
W h e n I w a s a t t h e z o o I s a w a m o n k e y a n d a n e l e p h a n t.
An obvious candidate solution would be the pattern "\b[(the)(a)(an)]\b". (Note: the sub-pattern "\b" is an anchor that matches a word boundary; it makes sure that we do not match the 'an' in the 'elephant'). But if we try it out, we see that it does not work. The sub-pattern "[(the)(a)(an)]" matches the following highlighted substrings:
The complete pattern "\b[(the)(a)(an)]\b" only matches the 'a' in front of 'monkey':
Possibly, we forgot the alteration construct and the solution is "\s[(the)|(a)|(an)]\s"? Wrong again. Try it for yourselves.
The reason is that the set-construct changes the meaning of the contained sub-expression (or sub-expressions), including the special characters. It transfers it to exactly that – a set of characters. So, our assumption that the pattern "[(the)(a)(an)]" would match against the string 'the' or the string 'a' or the string 'an' was incorrect. Instead, the pattern matches against any one character from the set that contains '(', 't', 'h', 'e', ')', '(', 'a', ')', '(', 'a', 'n' and ')'. From basic set theory we remember that the order and frequency of a set element does not matter, so we are really matching against a set consisting of the following characters: 'a', 'e', 'h', 'n', 't', '(', ')'.
The right RegEx pattern to find all English articles is this: "\b((the)|(an)|(a))\b".A test verifies this:
Another special character that is unexpectedly changed by the set operator is the any-character token ".". By itself it matches against any one input character, but in a set it becomes a literal and the pattern "[.]" matches against a dot.
To check whether you have understood this article, try to figure out the meaning of the following RegEx pattern. But no cheating! Think about it carefully first, before looking at the answer below.
And the answer is:
“ Select a text fragment that matches any one of the four following alternatives:
Did you get it right?Yes? Well done, you are a step closer to being a proficient RegEx developer.No? No worries. I must admit that when I first faced the above English article example, it took me quite a while to figure it out too. Keep practicing and check out some of the following material: