douqiao3930 2015-01-12 13:26
浏览 207
已采纳

在正则表达式中获取组的多个匹配项

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

  • 写回答

2条回答 默认 最新

  • duanlijia5864 2015-01-12 14:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!