But sometimes, writing them can be a serious pain in the ... you know where. Just the other day I had to write a little tool that does some serious find and replace stuff in generated code. At first I thought this would be a fairly easy job using regular expressions, but eventually I had to beat two issues that I really got stuck with for quite some time.
I was able to find 'the bugs' in my regex with this very simple, yet useful tool to test regular expression which I downloaded from here: http://www.regular-expressions.info/download/csharpregexdemo.zip
To demonstrate the issue, you can use the following input string:
testa
atest
testa
atest
... with the following regular expression pattern: test$ and the following replacement string: HELLO
When you hit the 'Replace' button, you get the result as expected: Only the last test in the string gets replaced.

Now, most of the time when you work with regular expressions, you work on a single line basis. But in my case I had to work accross multiple lines, so I used RegexOptions.Multiline. This option changes the way "^" and "$" work, so that they match the beginning and the end of each line in the input string instead of the beginning and end of the whole string the regex pattern is applied to. To demonstrate RegexOptions.Multiline in the tool, set the flag for the option '^ and $ match at embedded newlines'.

Now one might expect the first atest in my input string would be replaced by aHELLO as well ... but it didn't !
Now the sample I gave you is really straightforward, but the RegEx I worked uppon was fairly complex. So I'm gonna spare you the details on how long it took me to find the reason for this behavior and how many times I cursed at my computer during my quest, but eventually it all boiled down to this.
In Windows the second line ends with a carriage return and line feed character (\r\n). But, RegexOptions.Multiline works on ... you guessed it ... the new line character only, ignoring the good old carriage return altogether. So if you want the replacement to work correctly you could use something like the sample below to make the replacement correctly.

The second problem I handled was that my RegEx pattern started with ^(\s*) which was necessary to capture trailing spaces in a capture group to reuse them in the replacement string. It took me quite a while to realise that \s also captures newline characters as well, which sometimes resulted in unwanted extra empty lines in the replacement result.
By David Stroobants, .Net Solutions Architect