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("Letter = {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
http://www.regular-expressions.info/lookaround.html