Zooty's Funbag

March 5, 2009

.NET Regular Expression Reminder

Filed under: .net regex — zootfroot @ 9:31 am

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.

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.