Skip to content Skip to sidebar Skip to footer

Python - Regex Not Escaping Parentheses In Alphanumeric String With Symbols And A Variable Passed In To Method

This regex is supposed to find a string that finds something in this format exactly: 201308 - (82608) - MAC 2233-007-Methods of Calculus - Lastname, Lee.txt The only caveat is the

Solution 1:

Some things seem very strange for me:

  • \s\b\s is aberrant because \b means "Matches the empty string, but only at the beginning or end of a word`" but here it's between two symbols meaning whitespace, that is to say not at beginning or end of a word.

  • the antislash in [A-z\a-z] provokes an error. I wonder what it's supposed to mean here. Do you want an antislash as a possible character of the sett ? then write [A-z\\\\a-z]

This regex matches your example string:

r = re.compile(term +
               ("\s-\s"
                "\(\d{5}\)"
                "\s-\s"
                "\w{3}\s\d{4}-\d{3}-"
                "[a-zA-Z ]+"
                "\s-\s"
                "[A-za-z]+,\s"
                "[A-Za-z]+ *.txt"))

Solution 2:

\d{6}\s-\s\(\d{5}\)\s-\s\w{3}\s\d{4}-\d{3}-[^\.]+\.txt matches the string you sent in as an example. If the initial value is unknown, term + '\s-\s\(\d{5}\)\s-\s\w{3}\s\d{4}-\d{3}-[^\.]+\.txt' should do it (provided term plays nice for the regex).

adding a test run sample:

>>> term = '201308'
>>> f = '201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler, Lee.txt'
>>> re.search(term + '\s-\s\(\d{5}\)\s-\s\w{3}\s\d{4}-\d{3}-[^\.]+\.txt', f).group(0)
'201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler, Lee.txt'

yet another:

>>> f = '/somefolder/somefolder2/201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler, Lee.txt'
>>> re.search(term + '\s-\s\(\d{5}\)\s-\s\w{3}\s\d{4}-\d{3}-[^\.]+\.txt', f).group(0)
'201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler, Lee.txt'

>>> f = 'c:\\somefolder\\somefolder2\\201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler, Lee.txt'
>>> re.search(term + '\s-\s\(\d{5}\)\s-\s\w{3}\s\d{4}-\d{3}-[^\.]+\.txt', f).group(0)
'201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler, Lee.txt'

Post a Comment for "Python - Regex Not Escaping Parentheses In Alphanumeric String With Symbols And A Variable Passed In To Method"