Have trouble understanding capturing groups and back references

前端 未结 2 906
野趣味
野趣味 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:54

    \d is just one digit.

    This regular expression doesn't match the "123-456-7890" string but it would match "323" (which could be part of a greater string, for example "323-456-7890") :

     (\d) : first digit ("3")
     \d   : another digit ("2")
     \1   : first group (which was "3")
    

    Now, if your book pretends that (\d)\d\1 should capture "123" in "123-456-7890", then it might contain an error...

提交回复
热议问题