Search code examples
regexnotepad++

Find and Replace Words that have Uppercase letters in the middle using RegEx for Notepad++


I have a huge list of names. Each names are separated by newline and formatted with comma like the following

Lastname, Firstname
Lastname, Firstname
Lastname, Firstname
Lastname, Firstname

But there are instances where the newline was deleted so the Firstname and Lastname got merged like this example

Lastname, Firstname
Lastname, FirstnameLastname, Firstname
Lastname, FirstnameLastname, Firstname

Using regex in Notepad++, how can I find those instances and replace them with the correct name which is separated by a new line?

Thank you!

Edit: Here's what I tried and have been doing in the past hours.

\w+[A-Z]\w*

It works in finding all the instances of accidentally combined names. What I did is I just manually edit them one by one. From 500+ instances, I'm down to 300+. Still a long way to go. Tried searching online for similar pattern but to no avail. I hope I could do it in just one click, also this might help someone in the future who're stuck in the same scenario as me right now.

Update: The answer by toto works like a charm, so I accepted it as a correct answer. I learned something awesome today. Thanks Toto. Anyway, I already solved my problem using this approach a couple of minutes ago after experimenting with some regex I saw online.

Find: (\b[A-Za-z]+)([A-Z]+)([A-Za-z]+\b)
Replace: \1\n\2\3

It does the same thing but IMO Toto's approach is more elegant!


Solution

    • Ctrl+H
    • Find what: (?<=[a-z])(?=[A-Z])
    • Replace with: \n or \r\n depending on platform
    • TICK Match case
    • TICK Wrap around
    • SELECT Regular expression
    • Replace all

    Explanation:

    (?<=[a-z])    # positive lookbehind, make sure we have a small letter before
    (?=[A-Z])     # positive lookahead, make sure we have a capital after
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here