Search code examples
pythonregexsearchstr-replacepython-re

Python search for character pattern and if exists then indent


I have a pattern of text that I would like to find and push to a new line. The pattern is ), followed by a space and a character. Like this -

text_orig =

text cat dog cat dog
),
text rabbit cat dog
), text coffee cat dog. #need to indent this line

where it would become

text_new =

text cat dog cat dog
),
text rabbit cat dog
), 
text coffee cat dog

I'm pretty close to a solution, but stuck on what approach to use. Currently, I'm using re.sub but I believe that removes the first letter of the text like so -

text_new =

text cat dog cat dog
),
text rabbit cat dog
), 
ext coffee cat dog # removes first letter
re.sub('\),\s\w','), \n',text_orig)

Would I need search instead of sub? Help is very appreciated


Solution

  • You can use

    re.sub(r'\),[^\S\n]*(?=\w)', '),\n', text_orig)
    

    See the regex demo.

    Or, if the pattern should only match at the start of a line, you should add ^ and the re.M flag:

    re.sub(r'^\),[^\S\n]*(?=\w)', '),\n', text_orig, flags=re.M)
    

    Here,

    • ^ - start of a line (with re.M flag)
    • \), - a ), substring
    • [^\S\n]* - zero or more whitespaces other than LF char
    • (?=\w) - a positive lookahead that requires a word char immediately to the right of the current location.