ahill
ahill

Reputation: 1846

Notepad++ Find/Replace Regex Help

I am having issues doing a string replacement in Notepad++, and need some help.

My file:

LastName,(tab)FirstName[optional]MiddleName

Some times there is data that has a middle name, sometimes not.

Public,JohnQ.
Doe,John
Clinton,WilliamJefferson

would be:

Public(tab)John(tab)Q
Doe(tab)John
Clinton(tab)William(tab)Jefferson

I want to split it out into this:

LastName(tab)FirstName(tab)MiddleName

Upvotes: 2

Views: 5828

Answers (2)

BoltClock
BoltClock

Reputation: 723538

Thanks for adding the sample input. It helps immensely to have that around. Try this and see if it does what you want.

Find, making sure Match case is checked:

([A-Z][a-z]*),([A-Z][a-z]*)(.*)

Replace with:

\1(tab)\2(tab)\3

Of course, (tab) is actually a tab character that you have to place in the replacement string yourself.

Upvotes: 4

Czechnology
Czechnology

Reputation: 14992

An ugly regex like this works for me on the example you've provided:

(\w+),(\w+?)(([A-Z]\w*\.?)?)\n

replace with

\1\t\2\t\3\n

Note:

  • This only works if the middle name starts with a letter in the A-Z. You might be able to replace [A-Z] with [[:upper:]] if notepad++ supports it (I don't know).
  • I need that second bracket around the middle name part because I need to match at least an empty string when there is no middle name.

Upvotes: 0

Related Questions