Multiple results from one subgroup

前端 未结 1 1114
野趣味
野趣味 2021-01-25 12:45

I have this string: test
I want to catch all attributes with a regexp.

This regexp m

相关标签:
1条回答
  • 2021-01-25 13:27

    The short answer is you can't - only the last group is accessible. The Python docs state this explicitly:

    If a group matches multiple times, only the last match is accessible [...]

    You'll have to use some language features:

    1. In PHP, there's preg_match_all that returns all matches.
    2. In other languages, you'll have to do this manually: add the g modifier to the regex and loop over it. Perl, for example, will manage a string position and return the next match in $1 each time a /([...])/g pattern is matched.

    Also take a look at Capturing a repeated group.

    0 讨论(0)
提交回复
热议问题