Search code examples
pythonpython-re

regular expression to extract "5 rnd" from "5 Rnd (5-5-5-5-5)"


To put it simply, from "5 Rnd (5-5-5-5-5)", I need to extract the "5 Rnd" part, where the number could be any number, and the "Rnd" is sometimes written "rnd", sometimes "RND", or sometimes just "Rnd"


Solution

  • Try:

    txt - input text

    import re
    re.findall("\d+\s*rnd", txt, re.I)
    

    \d+ 1, or more digits

    \s* 0 or more spaces

    rnd letters "rnd"

    re.I case insensitive search