Search code examples
pythonpython-3.xpython-re

How to ignore ( in re module in Python


I have this re string in Python:

`r' \- (\d{4}) Remaster'`

And I want it to be something like this

r' - ((\d{4}) Remaster )'

I cannot put () in this string. I tried using \ (escape) like this

r' \- \((\d{4}) Remaster)'

but it of course won't work. I even tried re.escape

re.escape(r' \- (') + r'(\d{4}) Remaster)'

It also did not work

What Can I do?


Solution

  • When working with regular expressions (re) in Python, parentheses are used for grouping. If you want to include parentheses as part of the string you are matching (not for grouping), you need to escape them using backslashes (\).

    Here's how you can achieve your desired functionality:

    import re
    
    # The string you want to search
    test_string = "This is an example string - (2015 Remaster), another string - (2019 Remaster)"
    
    # The regular expression
    # The inner parentheses are used for grouping (so you can refer to the year separately if you want),
    # and the outer parentheses are part of the string you are matching.
    # The outer parentheses are escaped with backslashes (\) so they are interpreted as literal parentheses, 
    # not special characters.
    re_string = r' - \((\d{4}) Remaster\)'
    
    # Find all matches
    matches = re.findall(re_string, test_string)
    
    # Print all matches
    for match in matches:
        print(match)
    

    If you run this script, it should print out:

    2015
    2019
    

    Because those are the years in the " - (year Remaster)" parts of test_string. In this script, re.findall() is used to find all occurrences of the pattern in test_string. Each match is a string that was surrounded by parentheses in the regular expression, which in this case is a year (a sequence of 4 digits). The matches are then printed out, one per line.