问题
this is my pattern:
-{8}\s+((?:[0-9]{2}\/[0-9]{2}\/[0-9]{2})\s+(?:[0-9]{2}:[0-9]{2}:[0-9]{2}))\s+(?:LINE)\s+=\s+([0-9]{0,9})\s+(?:STN)\s+=\s+([0-9]{0,9})[ ]*(?:\n[ \S]*)*INCOMING CALL(?:[\S\s][^-])*
and this is my string:
-------- 02/16/18 13:50:39 LINE = 0248 STN = 629
CALLING NUMBER 252
NAME Mar Ant
UNKNOWN
DNIS NUMBER 255
BC = SPEECH
VOIP CALL
00:00:00 INCOMING CALL RINGING 0:09
LINE = 0004
00:00:25 CALL RELEASED
it does match with several online regex testers but not with C# testers like http://regexstorm.net/tester, since
.NET does not use Posix syntax
i notices that if i remove the last part of the pattern
INCOMING CALL(?:[\S\s][^-])*
it does match but still incorrect or at least not what i expect from it.
what should i change to make this pattern match the string ?
回答1:
The problem is not related to the pattern type, the pattern is not actually POSIX compliant as regex escapes cannot be used inside bracket expressions (see [\S\s]
in your pattern, which can be written as .
together with RegexOptions.Singleline
option, or as (?s:.)
).
All you need to do here is to replace \n
with \r?\n
or (?:\r\n?|\n)
to match Windows line break style.
Use
-{8}\s+((?:[0-9]{2}/[0-9]{2}/[0-9]{2})\s+(?:[0-9]{2}:[0-9]{2}:[0-9]{2}))\s+(?:LINE)\s+=\s+([0-9]{0,9})\s+(?:STN)\s+=\s+([0-9]{0,9})[ ]*(?:\r?\n[ \S]*)*INCOMING CALL(?:[\S\s][^-])*
See this regex demo.
来源:https://stackoverflow.com/questions/49106318/regex-c-sharp-doesnt-match-when-regex101-does