1

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.

MWeller
  • 105
  • 1
  • 13

2 Answers2

1

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 
Hailton
  • 1,192
  • 9
  • 14
  • 1
    Thank you, that is exactly what I was looking for. I was not sure if this is zero based, thank you for fixing this too. – MWeller Jul 23 '12 at 23:26
  • 1
    On top of this you can use named groups `new Regex("(?.*?)b(?.*)d(?.*?)"...` then `matcher.Groups["wibble"]` etc – Bob Vale Jul 23 '12 at 23:44
0
Regex rx = new Regex("(.*?)b(.*?)d(.*?)");
Match match = rx.Match("abcde");
string found = match.Value;
John Bonfardeci
  • 466
  • 3
  • 10
  • 1
    I already had something like this before, but it returns "abcd". It returns the last match of the regex, but I want to have a field or list with the text where the wildcard is, in this case "a" for the first wildcard, "c" for the second wildcard and so on. – MWeller Jul 23 '12 at 23:21