Have trouble understanding capturing groups and back references

前端 未结 2 909
野趣味
野趣味 2020-11-22 09:53

Wishing to put some order into my knowledge of regular expressions I decided to go through a book about them, Introducing Regular Expressions. And I know it\'s sill

2条回答
  •  面向向阳花
    2020-11-22 10:47

    (\d)\d\1 step by step:

    1. The first \d matches one digit
    2. And the parentheses () mark this as a capturing group - this is the first one, so the digit is remembered as "group 1"
    3. The second \d says there is another digit
    4. \1 says "here is the value from our previous group 1" - that is the digit that was matched in step 1.

    So like dystroy already said: the regex should match a sequence of three digits of which the first and the third are equal.

提交回复
热议问题