c# - RegexOptions.Multiline seems to ignore \n -
i'm trying search string using regex. here's string looks in text visualizer:
0 -12.67 td /helv 14 tf (source: abc / xyza) tj 0 -15.624 td (job source no.: grqx id 27299) tj 0 -15.624 td
when view value hovering on it:
0 -12.67 td\n/helv 14 tf\n(source: abc / xyza) tj\n0 -15.624 td\n(job source no.: grqx id 27299) tj\n0 -15.624 td
i'm using regex.matches()
following pattern , regexoptions.multiline
:
^(?<=[(]).+(?=[)])
this returns no matches. when omit caret, this:
(?<=[(]).+(?=[)])
then regex.matches()
returns both matches:
source: abc / xyza job source no.: grqx id 27299
how can match on first character in line?
a lookbehind in regex pattern checks characters left of current position, in pattern ^(?<=[(])
requires (
before ^
(the start of line). before start of line, there either nothing (at start of string), or there newline char. thus, never match string.
actually, not need lookarounds substrings need. use following regex regexoptions.multiline
option:
^\(([^()]+)\)
the ^
make sure match appears @ start of string, ([^()]+)
capture group 1 one or more chars other (
, )
, , )
matched.
see regex demo, results need in group 1.
in c#, use following code:
var res = regex.matches(str, @"^\(([^()]+)\)", regexoptions.multiline) .cast<match>() .select(m => m.groups[1].value) .tolist();
Comments
Post a Comment