这些不吸引人的正则表达式组为什么工作不正常? [英] Why aren't these non-capturing regex groups working right?

查看:50
本文介绍了这些不吸引人的正则表达式组为什么工作不正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在另一个堆栈溢出问题上花费了很多时间,而上一个问题也遇到了同样的问题.非捕获组无法正常工作,或者我相信.

So I spent a lot of time on another stack overflow question, and the same problem came up with a previous one. Non-capturing groups aren't working as I'd expect them to, or so I believe.

这是一个愚蠢的示例,类似于其他人的CSS测试字符串...

This is a silly example along the lines of someone else's CSS test string...

这是我的正则表达式:

(?:(rgb\([^)]*\)|\S+)(?:[ ]+)?)*

这是测试字符串:

1px solid rgb(255, 255, 255) test rgb(255, 255, 255)

我希望匹配组为"1px",纯色","rgb(255、255、255)",测试","rgb(255、255、255)"

I'm expecting match groups of "1px","solid", "rgb(255, 255, 255)", "test", "rgb(255, 255, 255)"

但是我只得到最后一个匹配的令牌.

But I'm only getting the last token matched.

这是测试链接:

http://regex101.com/r/pK1uG7

这是怎么回事?我以为我没有被俘虏的团体了,而regex101底部的解释方式是有意义的,包括贪婪".

What's going wrong here? I thought I had non-capturing groups down, and the way it's explained at the bottom of regex101 makes sense, including the "greediness".

推荐答案

捕获组将覆盖之前的每个匹配项.捕获组#1首先匹配"1px",然后捕获组#1匹配"solid",覆盖"1px",然后匹配"rgb(255,255,255)",覆盖"solid",等等.

The capture group overrides each previous match. Capture group #1 first matches "1px", then capture group #1 matches "solid" overwriting "1px", then it matches "rgb(255, 255, 255)" overwriting "solid", etc.

这篇关于这些不吸引人的正则表达式组为什么工作不正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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