Skip to content
Advertisement

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 –

JavaScript

where it would become

JavaScript

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 –

JavaScript
JavaScript

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

Advertisement

Answer

You can use

JavaScript

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:

JavaScript

Here,

  • ^ – start of a line (with re.M flag)
  • ), – a ), substring
  • [^Sn]* – 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.
Advertisement