Regex to swap piped key|value pairs

It's always useful to write down your regexs!

Let's say you had:

Private|Pvt
Private First Class|PFC
Lance Corporal|LCpl
Corporal|Cpl
Sergeant|Sgt
Staff Sergeant|SSgt
Gunnery Sergeant|GySgt
Master Sergeant|MSgt
First Sergeant|1stSgt
Master Gunnery Sergeant|MGySgt
Sergeant Major|SgtMaj
Sergeant Major of the Marine Corps|SgtMajMarCor
Warrant Officer 1|W-1
Chief Warrant Officer 2|W-2
Chief Warrant Officer 3|W-3
Chief Warrant Officer 4|W-4
Chief Warrant Officer 5|W-5
Second Lieutenant|2ndLt
First Lieutenant|1stLt
Captain|Capt
Major|Maj
Lieutenant Colonel|LtCol
Colonel|Col
Brigadier General|BGen
Major General|MajGen
Lieutenant General|LtGen
General|Gen

And you wanted to swap the value|key pairs to be key|value pairs. A quick regex with find and replace is a hell of a lot better than copy & pasting for an hour.

Find: ([^|]+)|([^\n]+)
Replace: \2|\1 (in notepad++)

and you get:

Pvt|Private
PFC|Private First Class
LCpl|Lance Corporal
Cpl|Corporal
Sgt|Sergeant
SSgt|Staff Sergeant
GySgt|Gunnery Sergeant
MSgt|Master Sergeant
1stSgt|First Sergeant
MGySgt|Master Gunnery Sergeant
SgtMaj|Sergeant Major
SgtMajMarCor|Sergeant Major of the Marine Corps
W-1|Warrant Officer 1
W-2|Chief Warrant Officer 2
W-3|Chief Warrant Officer 3
W-4|Chief Warrant Officer 4
W-5|Chief Warrant Officer 5
2ndLt|Second Lieutenant
1stLt|First Lieutenant
Capt|Captain
Maj|Major
LtCol|Lieutenant Colonel
Col|Colonel
BGen|Brigadier General
MajGen|Major General
LtGen|Lieutenant General
Gen|General

Thank goodness for regexes.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.