tags:

views:

45

answers:

3

Example:

\Doe, John\" <15553775555>"

The regex is \<(.*?)> and match <15553775555>. But it would be more accurate if it would return only what is inside the < >. How can we modified it to have what's inside? (By the way, what's inside can be not only a 12 digits...

+4  A: 

Use lookaheads and lookbehinds:

(?<=<)\d+(?=>)

Basically what this means is: find a sequence of one or more digits that is preceded by a < and followed by a >. You can of course just do:

<(\d+)>

because the parentheses mark a capturing group and then you just get that group rather than the entire match. Something like:

Regex regex = new Regex("<(\\d+)>");
Match match = regex.Match("Doe, John\" <15553775555>");
if (match.Success)
{
  String number = match.Groups[1].Value;
  Console.WriteLine("Found " + number);
}
else
{
  Console.WriteLine("No match found");
}
cletus
+1  A: 

Grab the sub match provided by the parens. It depends what regex engine you're using as to the syntax, but $1 works in .net I believe...

Myles
+2  A: 

Use a named capturing group :

Regex r = new Regex("<(?<number>.*?)>");
Match m = r.Match(input);
if (m.Success)
{
    string number = m.Groups["number"].Value;
    // whatever you need to do with it...
}

Or unnamed, if you prefer :

Regex r = new Regex("<(.*?)>");
Match m = r.Match(input);
if (m.Success)
{
    string number = m.Groups[1].Value;
    // whatever you need to do with it...
}
Thomas Levesque