\" 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" />
开发者

Regex how to get stuff between char without those chars?

开发者 https://www.devze.com 2022-12-11 08:44 出处:网络
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

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 digi开发者_高级运维ts...


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");
}


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...
}


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消