开发者

Regular Expression to search overlapping pattern

This is the simplified version of my problem:

I'm trying to extract all letters surrounded by non-word characters but my regex doesn't work when the non-characters overlap.

Here's my code:

var text = "Z#A#B#S";
var regex = new Regex(@"\W(?<letter>\w)\W");
foreach (var m in regex.Matches(text).Cast<Match>())
{
    Console.WriteLine("Match = {0}", m.Value);
    Console.WriteLine("L开发者_StackOverflow社区etter = {0}", m.Groups["letter"].Value);
    Console.WriteLine("-------------------");
}

I expected it to match both A and B but instead it only matches A. Here's the output:

Match = #A#
Letter = A
-------------------

This does work with the text "Z#A##B#S" (there's no overlap between the two matches).

How can I extract both A and B from the text "Z#A#B#S"?

Thanks


Use the look behind and the look ahead

        var text = "Z#A#B#S";
        var regex = new Regex(@"(?<=\W)\w(?=\W)");
        foreach (Match m in regex.Matches(text))
        {
            Console.WriteLine("Letter = {0}", m.Value);
            Console.WriteLine("-------------------");
        }

http://www.regular-expressions.info/lookaround.html


Probably you should use Lookaround assertions:

(?<=\W)(?<letter>\w)(?=\W)

Matches in the following way:

Regular Expression to search overlapping pattern

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜