Why are these non-engaging regular expression groups not working correctly?

So I spent a lot of time on another question, and the same problem arose from the previous one. Non-retaining groups do not work as I expected, or, as I believe.

This is a silly example from another CSS test string ...

Here is my regex:

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

And here is the test line:

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

I expect matching groups "1px", "solid", "rgb (255, 255, 255)", "test", "rgb (255, 255, 255)"

But I only get the last token.

This is the link for testing:

http://regex101.com/r/pK1uG7

What's going on here? I thought I didn’t have capture groups, and the way that he explains at the bottom of regex101 makes sense, including greed.

+4
2

. # 1 "1px", # 1 "" "1px", "rgb (255, 255, 255)" "solid" ..

+3

:

/(rgb\([^)]+\)|\S+)/g

http://regex101.com/r/kF2uV4

. , :

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

.

: , ? , (?:) ?

. . , , , .

+2

All Articles