How to match multiple occurrences of a substring

后端 未结 3 793
逝去的感伤
逝去的感伤 2020-11-22 17:00

If I have an HTML string such as:

£2056

<
3条回答
  •  [愿得一人]
    2020-11-22 17:26

    Adding /g isn't enough if you with to match multiple occurrences of a substring. If that's the case reluctant quantifiers may be used as described here.

    Given the string:

    £2056

    You will arrive at the text you wanted using:

    \d+.*>\d+
    

    But given the same string repeated two times:

    £2056

    £2056

    You will not find the target selection multiple times. You'll only find it once due to the greedy nature of .*. To make .* non-greedy, or reluctant, simply add a ? after the * and you will arrive at:

    \d+.*?>\d+
    

    Which will find both occurrences of the substring you asked for as shown here.

提交回复
热议问题