负前瞻在带有加号量词的字符范围后不起作用 [英] Negative lookahead not working after character range with plus quantifier

查看:52
本文介绍了负前瞻在带有加号量词的字符范围后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个正则表达式,其中包含所有具有任意数量单词但不能后跟 : 的字符串,如果匹配则忽略匹配.我决定用消极的眼光看待它.

I am trying to implement a regex which includes all the strings which have any number of words but cannot be followed by a : and ignore the match if it does. I decided to use a negative look ahead for it.

/([a-zA-Z]+)(?!:)/gm
string: lame:joker

因为我使用的是字符范围,所以它一次匹配一个字符,并且只忽略 : 之前的最后一个字符.在这种情况下,我如何忽略整个匹配项?

since i am using a character range it is matching one character at a time and only ignoring the last character before the : . How do i ignore the entire match in this case?

regex101 链接:https://regex101.com/r/DlEmC9/1

Link to regex101: https://regex101.com/r/DlEmC9/1

推荐答案

+ 之后做一个词边界检查 \b 以要求它到达结尾这个词.

Do a word boundary check \b after the + to require it to get to the end of the word.

([a-zA-Z]+\b)(?!:)

这是一个运行示例.

这篇关于负前瞻在带有加号量词的字符范围后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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