Extracting parts of text using Regex in c #

advertisements

In Java I could simply extracts parts of codes or texts using Regex, but in C# I'm not sure how I could do this. For example:

Pattern p = Pattern.compile("(.*?)b(.*?)d(.*?)", Pattern.DOTALL|Pattern.MULTILINE);
Matcher matcher = p.matcher("abcde");
System.out.println(matcher.group(1));   //c

I've found lots of tutorials about Regex in C# but I found no way to do something simple like this. But it has to work with every kind and number of text, numbers, symbols and so on, so that I can use it for example to extract parts of HTML code.


First of all. Your code in java does not work as is.

Correct code (tested):

Pattern p = Pattern.compile("(.*?)b(.*?)d(.*?)", Pattern.DOTALL|Pattern.MULTILINE);
Matcher matcher = p.matcher("abcde");
if (matcher.find())  // Without this test: [Exception in thread "main" java.lang.IllegalStateException: No match found]
  System.out.println(matcher.group(2));   //c     <--2 instead of 1

Equivalent in C # (tested):

Regex r = new Regex("(.*?)b(.*?)d(.*?)", RegexOptions.Singleline | RegexOptions.Multiline);
Match matcher = r.Match("abcde");
Console.WriteLine(matcher.Groups[2]);   //c