Lazy quantifier {,}? not working as I would expect

前端 未结 3 400
忘了有多久
忘了有多久 2020-12-04 02:37

I have an issue with lazy quantifiers. Or most likely I misunderstand how I am supposed to use them.

Testing on Regex101 My test string is let\'s say: 12345678

3条回答
  •  猫巷女王i
    2020-12-04 02:57

    If you have a string containing numbers followed by a non-numeric, the minimum set of {1,5}? would always be 1 (so it's not necessary to have the range.) I don't think the lazy operator is actually working as we think on the numeric range.

    If you make the first \d+ greedy, as below you'll get the minimum number of digits before the D.

    (\d+)(\d{1,5}D) Matches 9D in the second group

    If you make the first set of numbers lazy, then you'll get the maximum number of digits (5)

    (\d+?)(\d{1,5}D) Matches 56789D in the second group

    I think these regex expressions might be more in line with what you need.

提交回复
热议问题