Reuse part of a Regex pattern

前端 未结 6 1566
北海茫月
北海茫月 2020-11-28 14:40

Consider this (very simplified) example string:

1aw2,5cx7

As you can see, it is two digit/letter/letter/digit values separated

6条回答
  •  天命终不由人
    2020-11-28 14:56

    Note: this will work with PyPi regex module, not with re module.

    You could use the notation (?group-number), in your case:

    (\d\w\w\d),(?1)
    

    it is equivalent to:

    (\d\w\w\d),(\d\w\w\d)
    

    Be aware that \w includes \d. The regex will be:

    (\d[a-zA-Z]{2}\d),(?1)
    

提交回复
热议问题