Search code examples
pythonregexstringnlp

Insert space if uppercase letter is preceded and followed by one lowercase letter - Python


Is there a way to insert aspace if it contains a uppercase letter (but not the first letter)?

For example, given "RegularExpression" I´d like to obtain "Regular Expression".

I tried the following regex:

re.sub("[a-z]{1}[A-Z][a-z]{1}", " ","regularExpression") 

Unfortunately, this deletes the matching pattern:

regula pression

I would prefer a regex solution, yet would be thankful for any working solution. Thanks!


Solution

  • You can do this with the following:

    import re
    
    s = "RegularExpression"
    re.sub(r"([A-Z][a-z]+)([A-Z][a-z]+)", r"\1 \2", s)
    

    which means "put a space between the first match group and the second match group", where the match groups are a cap followed by one or more non-caps.