C# regex: negative lookahead fails with the single line option

Posted by Sylverdrag on Stack Overflow See other posts from Stack Overflow or by Sylverdrag
Published on 2010-06-01T08:04:59Z Indexed on 2010/06/01 8:13 UTC
Read the original article Hit count: 255

Filed under:
|
|

I am trying to figure out why a regex with negative look ahead fails when the "single line" option is turned on.

Example (simplified):

<source>Test 1</source>
<source>Test 2</source>
<target>Result 2</target>
<source>Test 3</source>

This:

<source>(?!.*<source>)(.*?)</source>(?!\s*<target)

will fail if the single line option is on, and will work if the single line option is off. For instance, this works (disables the single line option):

(?-s:<source>(?!.*<source>)(.*?)</source>(?!\s*<target))

My understanding is that the single line mode simply allows the dot "." to match new lines, and I don't see why it would affect the expression above.

Can anyone explain what I am missing here?

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex