perl正则以匹配结肠发生的情况如果存在
如果第一个单词包含“蓝色”,则我想将REGEX与字符串中的任何字母/number/下划线匹配以及双结肠匹配。
例如,
Blue Red Yellow //return Red
Blue Red::Orange Yellow //return Red::Orange
Purple Red Yellow //return nothing
Blue R_E_D //return R_E_D
Red Blue //return nothing
Blue.ish Yellow //return Yellow
我尝试了/blu \ s+\ s+(\ w+)/
,除了 ::
案例外,它都可以使用。在检查 W+
之后,我如何添加匹配项,如果存在 W+
,也可以匹配双结肠,而不必要求我的等级仅在 ::
也存在的情况下匹配。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用捕获组并重复单词char或:
模式匹配:
^
或使用\ b
(如果不是在开始蓝色\ b \ s*匹配单词
blue
和可选的非白色whitspace cahrs\ h+
匹配1+空格([\ w:]+)
捕获1+字char或:在第1组中REGEX DEMO
或使用
\ k
清除匹配缓冲区:regex demo
如果该单词不能以结肠开头,但应该具有
::
在之间和bluebird
之间也应如 @ ikegami :You can use a capture group and repeat the word char or
:
The pattern matches:
^
Or use\b
if not at the startBlue\b\S*
Match the wordBlue
and optional non whitspace cahrs\h+
Match 1+ spaces([\w:]+)
Capture 1+ word chars or:
in group 1Regex demo
Or using
\K
to clear the match buffer:Regex demo
If the word can not start with a colon but should have
::
in between andBluebird
should also match as noted in the comments by @ikegami:Regex demo