Leustad
Leustad

Reputation: 385

Swap text around a character

I have lines of text following the same format:

Title1 : Text1 Title2 : Text2

How can I swap the text at both sides of : to the other side? Like:

Text1 : Title1 Text2 : Title2

I can write a small script to do this but I was wondering if there is an quicker way to do it with Regex, a built in functionality or any other magic?

Upvotes: 1

Views: 738

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

You may use

^([^\r\n:]+)(:\h*)(.*)$

or

^([^\r\n:]+)(:\h*)([^:\r\n]+)$

Replace with $3$2$1 where the $n are backreferences to the values captured with the capturing groups. The groups are numbered automatically in the order they appear in the pattern.

Details:

  • ^ - start of line
  • ([^\r\n:]+) - Group 1 later referenced to as $1 capturing one or more chars other than CR, LF and colon
  • (:\h*) - Group 2 capturing a colon and zero or more horizontal whitespaces
  • ([^:\r\n]+) -Group 3 referenced to as $3 later. See above.
  • $ - end of line.

Upvotes: 1

nobody
nobody

Reputation: 11080

Step 1: Hold Alt button on the keyboard and highlight the 3rd column i.e. Text1. This will select the entire 3 column.

Step 2: Right Click and choose 'Cut' and then Paste in front of the 1st column.

Step 3: Hold Alt button on the keyboard and highlight the ':' column.

Step 4: Right Click and choose 'Cut' and then Paste in between the 1st and 2nd column.

Upvotes: 0

Related Questions