.NET Regex Negative Lookahead - What am I doing wr

2019-09-20 09:43发布

This question already has an answer here:

I asked this previously and used what I believe to be entirely too simple of a construct, so I'm trying again...

Assuming that I have:

This is a random bit of information from 0 to 1.
  This is a non-random bit of information I do NOT want to match
This is the end of this bit

This is a random bit of information from 0 to 1.
  This is a non-random bit of information I do want to match
This is the end of this bit

And (attempting) the following regex:

/This is a random bit(?:(?!NOT).)*?This is the end/g

Why is this not matching?

Regexr.com link: http://regexr.com/3db8m

What I'm looking to accomplish:

1) Determine a match based on a partial string of a line

2) Determine a match that ends with a partial string of a line

3) NOT capture based on some random string inside that start/end of a match.

edit

The patterns suggested in the original question were entirely too complicated for my meager understanding of Regex. Further, the suggestion of (?s) was throwing errors on regexr.com (ERROR: Invalid target for quantifier), so I reconstructed the question here.

If, indeed, there is a method to edit a question once asked, I apologize for not finding the edit link. I did find this edit link, as this question was marked as a 'duplicate' and 'previously answered'.

Respectfully, an answer not understood is no answer. As the author and seeker of the information contained in both this, and my previous question, I state that Maximilian Gerhardt's answer was the more correct (for me, at least).

Also, no idea if this is what was expected of this edit? I usually resort to StackOverflow when I've left a large enough dent on my desk. If I'm mis-using the site, again, I apologize :)

1条回答
Juvenile、少年°
2楼-- · 2019-09-20 09:50

Don't use . with text that has newlines in it.. Working example:

http://regexr.com/3db92

enter image description here

查看更多
登录 后发表回答