远负回顾 [英] Far Negative Lookbehind

查看:39
本文介绍了远负回顾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改我的模式以在字符串中往后看?

How do I change my pattern to look further back in the string?

当段落"一词在模式 (alpha) 后面时,则匹配为假,否则为真.

When the word "paragraph" is behind the pattern (alpha) then the match is false, otherwise it's true.

例如,这将有 5 个匹配:

代表第 (a)、(b)、(c)、(d) 或(e) (f) 段;

acts on behalf of any person referred to in act (a), (b), (c), (d) or (e) paragraph (f);

它们将是:(a) (b) (c) (d) (e)

这将有 0 个匹配:

代表 (a)、(b)、(c) 段中提及的任何人行事,(d) 或 (e);

acts on behalf of any person referred to in paragraph (a), (b), (c), (d) or (e);

推荐答案

这对于任何长度的简单回顾都是不可能的.Java regex 风格只允许有限长度的lookbehind(即你可以做 (?<=x{2,10}) 而不是 (?<=x*)).

That's not possible for any length with a simple lookbehind. Java regex flavor only allow finite-length lookbehind (i.e. you can do (?<=x{2,10}) but not (?<=x*)).

如果你能把问题简化为(a)前面的100个字母中不应出现paragraph这个词",那就行了:

If you can reduce the problem to "the word paragraph should not appear in the 100 letters preceding (a)", that works:

(?<!paragraph.{0,100})\([a-z]\)

如果你真的想要无限距离并且如果你的正则表达式很灵活并且可以从输入的开头开始并且只匹配一个(ref),你可以近似具有负前瞻的想要的行为(不必是有限的):

If you really want unbounded distance and if your regex is flexible and can start at the beginning of the input and match only one (ref), you can approximate the wanted behavior with negative look-ahead (which needs not be finite):

^(?!.*?paragraph.*?\([a-z]\)).*?\([a-z]\)

将匹配 test test (a) 但不匹配 paragraph test (a).

Will match test test (a) but not paragraph test (a).

这是一个技巧,虽然维护起来会变得非常复杂,但也有缺点(比如只匹配一次),最终可能有更好的方法来解决您的问题.例如,您可以匹配所有那些 ([a-z]) 然后检查字符串是否包含 paragraph,消除其位置之后的所有匹配项.

That is a trick though that can become quite complex to maintain, has downsides (like matching only once) and ultimately there are probably better ways to solve your problem. For instance, you could match all those ([a-z]) then check whether the string contains paragraph, eliminating all matches that come after its position.

PS:代替Pattern.compile("[aA][bB][cC]"),考虑使用Pattern.compile("abc", Pattern.CASE_INSENSIVE)code> 或 Pattern.compile("(?i)abc")(如果整个正则表达式不区分大小写)或 Pattern.compile("(?i:abc)dEf")(如果只有 abc 不区分大小写).

PS: instead of Pattern.compile("[aA][bB][cC]"), consider using Pattern.compile("abc", Pattern.CASE_INSENSISIVE) or Pattern.compile("(?i)abc") (if the whole regex is case-insensitive) or Pattern.compile("(?i:abc)dEf") (if only abc is case-insensitive).

这篇关于远负回顾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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