简体   繁体   中英

Get multiple matches of a group in regular expression

I am trying to get attributes from a BBCode using regular expression Lets say I have those strings:

[icons a="1" b='2' c="3"]
[icons d="4" e='5' f="6"]

I need using a regular expression to get matches like this:

[
    {'a', '1', 'b', '2', 'c', '3'},
    {'d', '4', 'e', '5', 'f', '6'}
]

I figured this regular expression:

\[icons[\w\s](?:(\w*)\=["|']([^"|']*))+.*?\]

But it only matches:

[
    {'a', '1'},
    {'d', '4'}
]

How I can match all the attributes?

Also, just to make it more bullet proof, is there a way to match only the quote it found?

[icons a="1"] // GOOD
[icons a="1'] // BAD - But still a match

Regex101

I think the best way is to keep it simple:

Use one regex to match all the [icons...] :

\[icons\b[^\[\]]*\]

and a second regex to be used iteratively on each of this regex' matches:

(\w+)=(['"])((?:(?!\2).)*)\2

Note that the first and third group will contain your desired values, the second group contains the quote.

您必须使用它,这可能会帮助您

\[icons\s(\w)[=]["|'](\d)["|']\s(\w)[=]["|'](\d)["|']\s(\w)[=]["|'](\d)["|']\]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM