EmaSiena
EmaSiena

Reputation: 23

Regex - How can I swap two expressions in different lines?

I'm pretty new in Regex, but I want to swap two expressions in two different lines. Here's the code:

translate italian ch1_a20cefa7:|

# "..."
msgstr "..."

I'd like to swap in:

translate italian ch1_a20cefa7:msgstr

# "..."
| "..."

As you can see, the | has been swapped with the msgstr part down there. How can I do it? Also, there are multiple lines where I should apply this, and sometimes in front of the | you can find other letters or characters. I was thinking about this: ([|].*) for the first part, but then I don't know how to find the msgstr (that it never changes).

Edit:

Here's other example of the code I'm talking about.

translate italian ch1_669664cb:| mc

# "Hey."
msgstr "Hey."


translate italian ch1_177d3eb2:| mc

# "What are you doing?"
msgstr "What are you doing?"


translate italian ch1_25d98ea6:| m p_1a

# "I'm in charge of the breakfast, remember?"
msgstr "I'm in charge of the breakfast, remember?"


translate italian ch1_7ae83e04:|

# "Oh, that's right."
msgstr "Oh, that's right."

I want it swapped like this:

translate italian ch1_669664cb:msgstr 

# "Hey."
| mc "Hey."


translate italian ch1_177d3eb2:msgstr 

# "What are you doing?"
| mc"What are you doing?"


translate italian ch1_25d98ea6:msgstr 

# "I'm in charge of the breakfast, remember?"
| m p_1a"I'm in charge of the breakfast, remember?"


translate italian ch1_7ae83e04:msgstr 

# "Oh, that's right."
|"Oh, that's right."

As you can see, sometimes there's something in front of the | but sometimes there's not.

Upvotes: 2

Views: 1707

Answers (3)

C Würtz
C Würtz

Reputation: 864

replace /^(translate .*):\|$/ => $1:msgstr then replace all /^msgstr\b/ => |

I'm not sure we are understanding your question. Can you extend your example?

EDIT: I'm late, can you test:

Search: /^(translate\b[^:]*:)([^\n]*)\n(.*)msgstr/gmsU
Replace: $1mgstr\n$3$2

Demo: https://regex101.com/r/jZekRH/1

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163362

One option is to use 4 capturing groups and switch group 4 and 2 in the replacement

^(.*)(\|.*)(\n(?:(?!msgstr).*\n)*)(msgstr)
  • ^ Start of string
  • (.*) Capture group 1, match any char 0+ times
  • (\|.*) Capture group 2 from the last occurrence of | until the end of the string in group 2
  • (\n(?:(?!msgstr).*\n)*) Capture group 3, match all lines that don't start with msgstr
  • (msgstr) Capture group 4, match msgstr

Replace with

$1$4$3$2

Regex demo

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521289

You may try the following find and replace, in regex mode:

Find:    \|(.*?)msgstr
Replace: msgstr$1|

Demo

The regex pattern matches a single pipe |, then it captures all content, across lines, until hitting the first instance of msgstr. The replacement does the swap, with the pipe and msgstr swapping positions, with the captured content in between them unchanged.

Edit:

My answer assumes you are doing your regex replacement in dot all mode, where .* matches across lines. If not, then use the following pattern instead:

Find:    \|([\s\S]*?)msgstr
Replace: msgstr$1|

Note: This answer was given to the original question, which the OP has dramatically changed since I posted this answer.

Upvotes: 0

Related Questions