Search code examples
pythonpython-3.xregexpython-re

How to sub a string with a capturing group that does not end with white space but might have white space after the capturing group?


I am trying to replace a string that looks like this ( self, False ) to (self, False). The regex I am using:

s = re.compile('\(\s*(.*)\s*\)')
s.sub(r'(\1)', '(    self, False   )')

Which returns (self, False )

How do I capture the group inside the parentheses without the trailing white spaces?


Solution

  • Found a simple solution.

    s = re.compile('\(\s*(.*?)\s*\)')
    s.sub(r'(\1)', 'hi hello ble ble ( self, False   ) ( self      ) (self , greedy    ) (    hello)')
    #Output
    'hi hello ble ble (self, False) (self) (self , greedy) (hello)'
    

    According to python re documentation:

    The '', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.> is matched against ' b ', it will match the entire string, and not just ''. Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using the RE <.*?> will match only ''.