Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to move beginning of string to end in Notepad++

I have a textfile like so:

marc_webber
john_grisham
rahmin_darfur
firstname_lastname

I want the output to be like this (forget about double names like "van These"):

Webber, Marc, marc_webber
Grisham, John, john_grisham
Darfur, Rahmin, rahmin_darfur
LastName, FirstName, firstname_lastname

So I want to split the strings at the _, move the last name to the beginning and comma delimit first name and the concatenated name to the end (and maybe even capitalize the first letter). This would be fairly easy with a programming language, but I want to know whether it is possible using Notepad++'s find and replace feature with regular expressions.
Basically I would need to create variables for the first name and the last name and string them together again in the end.

like image 647
Dennis G Avatar asked Sep 17 '25 16:09

Dennis G


2 Answers

Except for the casing this should work (Tested in Programmers Notepad though...):

Find Pattern:

((\w+)_(\w+))

Replace Pattern:

\3, \2, \1
like image 82
Stefan Avatar answered Sep 19 '25 04:09

Stefan


This worked for me using sed (I guess it also works in notepad++ since it probably supports perl regular expressions):

$ sed -r 's/(\w+)_(\w+)/\u\2, \u\1, \0/' file.txt
Webber, Marc, marc_webber
Grisham, John, john_grisham
Darfur, Rahmin, rahmin_darfur
Lastname, Firstname, firstname_lastname

The trick to get the capitalization as in your example is to use \u.

You can find more information about escape sequences here.

like image 36
jcollado Avatar answered Sep 19 '25 06:09

jcollado