.NET Regex Negative Lookahead - What am I doing wrong (round 2!) [duplicate]

旧巷老猫 提交于 2019-12-13 10:28:18

问题


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:


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

http://regexr.com/3db92



来源:https://stackoverflow.com/questions/36969519/net-regex-negative-lookahead-what-am-i-doing-wrong-round-2

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