简体   繁体   中英

RegEx C# doesnt Match when regex101 does

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 ?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM