前瞻vs后向 [英] Lookahead vs lookbehind

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

问题描述

我很难理解先行"和后行"的概念.例如,有一个字符串"aaaaaxbbbbb". 如果我们看"x",那么向前看是否意味着朝"bbbbb"或"aaaaa"看"x"?我是说方向.

I have a hard time to understand the concepts of "lookahead" and "lookbehind". For example, there is a string "aaaaaxbbbbb". If we look at "x", does lookahead mean looking "x" towards "bbbbb" or "aaaaa"? I mean the direction.

推荐答案

如果正则表达式为x(?=insert_regex_here),则表示(正)外观*超前*,即表示超前前进,换句话说就是朝着"bbbb"前进. 这意味着找到一个后跟 insert_regex_here的x".

If the regex is x(?=insert_regex_here) that is a (positive) look*ahead*, which looks ahead, or forwards, in other words towards "bbbb". It means "find an x that is followed by insert_regex_here".

如果正则表达式为(?<=insert_regex_here)x,则表示(正)外观*在后面*,它在后面向后,换句话说,朝向"aaaa".意思是找到一个 insert_regex_here之前的x".

If the regex is (?<=insert_regex_here)x that is a (positive) look*behind*, which looks behind, or backwards, in other words towards "aaaa". It means "find an x that is preceded by insert_regex_here".

您还可以使用负向前瞻x(?!insert_regex_here)表示"x 不是,后跟insert_regex_here",以及负向后视(?<!insert_regex_here)x,表示"x not insert_regex_here"之前.

You can also have negative lookahead x(?!insert_regex_here) meaning "x not followed by insert_regex_here", and negative lookbehind (?<!insert_regex_here)x, meaning "x not preceded by insert_regex_here".

(上面的(?=(?<!等都是Perl regex语法-语法可能会略有不同,具体取决于您的regex风格).

(The above (?= and (?<! etc are Perl regex syntax - the syntax might be slightly different depending on your flavour of regex).

我建议您阅读乍得在评论中提供的链接. 有示例.

I recommend you read the link that Chad gave in the comments. It has examples.

这篇关于前瞻vs后向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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