0

I'm looking to find and replace "agoWhen" with:

"ago\nWhen"

I can find with [a-z][A-Z], but how do I replace with a line break?

1 Answers1

0

I think you can use regex to find the pattern and then apply for loops to get the desired format.

def insert_new_line(text):
    result = []
    for match in re.findall('[a-z]+[A-Z][a-z]+', text):
        for index in range(len((match))):
            if match[index].isupper():
                result.append(match[:index]+'\n' +match[index:])
    #return resresult
    return ' '.join(result)

For example;

print(insert_new_line('thisMan is not inClass'))

returns;

this
Man in
Class

And if you want a list of all the matches returned, you could simply replace the return line of the function with;

return result

in which case;

print(insert_new_line('thisMan is not inClass'))

returns;

['this\nMan', 'in\nClass']

Let me know if this helps.

Samuel Nde
  • 2,565
  • 2
  • 23
  • 23