regex - Regular Expression to search overlapping pattern -
this simplified version of problem:
i'm trying extract letters surrounded non-word characters regex doesn't work when non-characters overlap.
here's 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 match both , b instead matches a. here's output:
match = #a# letter = -------------------
this work text "z#a##b#s" (there's no overlap between 2 matches).
how can extract both , b text "z#a#b#s"?
thanks
use behind , 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("-------------------"); }
Comments
Post a Comment