What is the C# equivalent of java.util.regex?

Posted by peter.murray.rust on Stack Overflow See other posts from Stack Overflow or by peter.murray.rust
Published on 2009-10-17T16:34:23Z Indexed on 2010/05/17 7:10 UTC
Read the original article Hit count: 343

Filed under:
|
|

I am converting Java code to C# and need to replace the use of Java's regex. A typical use is

import java.util.regex.Matcher;
import java.util.regex.Pattern;
//...

String myString = "B12";
Pattern pattern = Pattern.compile("[A-Za-z](\\d+)");
Matcher matcher = Pattern.matcher(myString);
String serial = (matcher.matches()) ? matcher.group(1) : null;

which should extract a capture group from a matched target string. I'd be grateful for simple examples.


EDIT: I have now added the C# equivalent of the code as an answer.

EDIT: Here is a tutorial on the use of the actual expressions.

EDIT: Here is a useful comparison of C# and Java (and Perl.)

© Stack Overflow or respective owner

Related posts about c#

Related posts about java