Home  >  Q&A  >  body text

python - How to match multiple strings with regular regularity instead of a single letter

For example, in the following string, multiple repetitions of 'asd' are matched 'asdasdasdasdasdasdasdasd' instead of a single 'asd'
string='asdasdasdasdasdasdasddasfdhgasghsd'
Thank you!

怪我咯怪我咯2712 days ago764

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-18 10:49:24

    string = 'asdasdasdasdasdasdasddasfdhgasghsd'
    print(re.findall(r'((?:asd)+)', string))
    
    # 解释下
    # (?:asd)+ 是正则的一种不存分组的语法, 它具有2个用途, 将`asd`看成一个样式整体, 所以当我们用+时, 就能代表多个asd
    # () 最外层的括号就是将匹配的结果存入分组, 与上面不同的就是, 少了`?:`, 因为没有这个, 所以它能存到分组
    # 所以整体的结果就是: 将多个asd匹配, 并存入分组, 然后在re.findall的结果就能看到了

    reply
    0
  • 为情所困

    为情所困2017-05-18 10:49:24

    /asd/g

    reply
    0
  • Cancelreply