regex101匹配时,RegEx C#不匹配 [英] RegEx C# doesnt Match when regex101 does
问题描述
这是我的模式
-{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][^-])*
这是我的字符串:
-------- 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
它确实与一些在线正则表达式测试人员匹配,但与诸如 http://regexstorm.net/tester 的C#测试人员不匹配. a>,因为
it does match with several online regex testers but not with C# testers like http://regexstorm.net/tester, since
.NET不使用Posix语法
.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 ?
推荐答案
问题与模式类型无关,该模式实际上与POSIX不兼容,因为正则表达式转义不能在方括号表达式内使用(请参见您的[\S\s]
模式,可以与RegexOptions.Singleline
选项一起写为.
,也可以写为(?s:.)
).
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:.)
).
您要做的就是将\n
替换为\r?\n
或(?:\r\n?|\n)
以匹配Windows换行样式.
All you need to do here is to replace \n
with \r?\n
or (?:\r\n?|\n)
to match Windows line break style.
使用
-{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][^-])*
请参见 查看全文