5 Mar 2009

.NET Regular Expression Reminder

I must be dense - I *heart* regex but every time I need to use one in .NET, I spend about 5 minutes trying to remember

  • what the right grouping character is
  • whether I need to escape it or not
  • which Regex class to start with
  • what the right terminology is for getting the sub-matches out of the match etc.
Which is weird cos when I was king of perl I could do this stuff lickity split. So I guess MS just like making it confusing. Evidence 1 - the regex syntax in Visual Studio is totally different to that in .NET. Duh.

Anyway here's a handy reminder to myself:

string strServer = System.Text.RegularExpressions.Regex.Match(strCon,"Data Source=(.+?);").Groups[1].Value;


Note the (.+?);
The trailing ? forces the match not to be greedy and only match up to the first semi colon.

No comments:

Post a Comment

Comments are very welcome unless you're a spammer, in which case you should probably kill yourself.

If I helped you out today, you can buy me a beer below. Cheers!