.NET正则表达式负提前-我在做什么错(第2轮!) [英] .NET Regex Negative Lookahead - What am I doing wrong (round 2!)

查看:131
本文介绍了.NET正则表达式负提前-我在做什么错(第2轮!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过这个问题,并使用了我认为对结构完全太简单的内容,所以我再次尝试...

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

假设我有:

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

为什么不匹配?

Regexr.com链接: http://regexr.com/3db8m

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

我要完成的任务:

1)根据一行的部分字符串确定匹配项

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

2)确定以一行的部分字符串结尾的匹配项

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

3)不根据比赛开始/结束内的某些随机字符串进行捕获.

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

编辑

对于我对Regex的微不足道的理解,原始问题中建议的模式完全太复杂了.此外,(?s)的建议是在regexr.com上引发错误(错误:量化目标无效),因此我在此处重新构建了问题.

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'.

可以肯定的是,无法理解的答案就是没有答案.作为本文档以及上一个问题的作者和寻求者,我声明马克西米利安·格哈特(Maximilian Gerhardt)的回答更为正确(至少对我而言).

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).

此外,不知道这是这次编辑的预期结果吗?当我在桌子上留下足够大的凹痕时,通常会求助于StackOverflow.如果我滥用网站,我再次表示歉意:)

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 :)

推荐答案

不要对包含换行符的文本使用.. 工作示例:

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

http://regexr.com/3db92

这篇关于.NET正则表达式负提前-我在做什么错(第2轮!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆