正向超前无法按预期工作 [英] Positive lookahead not working as expected

查看:50
本文介绍了正向超前无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的正则表达式如下:

/black(?=hand)[ s]/

我希望它匹配黑手黑手.但是,它不匹配任何内容.我正在 Regex101 上进行测试.

I want it to match blackhands or blackhand. However, it doesn't match anything. I am testing on Regex101.

我在做什么错了?

推荐答案

Lookahead不会消耗正在搜索的字符串.这意味着[ s]试图匹配紧随 black 之后的空格或 s .但是,您的前瞻性提示 hand 必须紧跟 black ,这样正则表达式永远无法匹配任何内容.

Lookahead does not consume the string being searched. That means that the [ s] is trying to match a space or s immediately following black. However, your lookahead says that hand must follow black, so the regular expression can never match anything.

要在使用先行提示时匹配黑手黑手,请在先行提示内移动[ s]:black(?=hand[ s]).另外,也不要使用前瞻功能:blackhand[ s].

To match either blackhands or blackhand while using lookahead, move [ s] within the lookahead: black(?=hand[ s]). Alternatively, don't use lookahead at all: blackhand[ s].

这篇关于正向超前无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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