RegEx C# doesnt Match when regex101 does

放肆的年华 提交于 2020-01-24 09:03:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!