Ask Your Question

How do I swap phrases using regex

asked 2019-11-08 03:34:15 +0200

Chuck D. gravatar image

updated 2019-11-08 03:35:06 +0200

I have a documet that I have the following phrases:

Form "Video Adjustments"

Form "Users"

Form "Audio Settings"

...and I have to swap the terms with form:

"Video Adjustments" Form

"Users" Form

"Audio Settings" Form

I can find the phrase with the following form:

Form "*"



But when I try to put in the replacement:

"*" Form

It only puts "*" Form, but of course does not have the original phrase.

How do I put in the proper terms for Search and replace?


Chuck D.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2019-11-08 06:19:48 +0200

keme gravatar image

updated 2019-11-08 11:43:51 +0200

To reference partial matches in a regular expression, you need to first mark a section to match by enclosing it in parentheses. Each parenthesis group will then be numbered in sequence, and you reference it by $<number>.

Also, in regular expressions, the asterisk is not the "any number of any character" wildcard, but rather "any number of the preceding character". The token for "any character" is a period.

Hence, wildcard * (as used by default in MS Word) translates to regex .*.

Try this:

  • Search for (Form) (".*")
  • Replace with $2 $1

If the phrase requiring rearrangement always constitutes a full paragraph, you may benefit from some additional code to avoid matches in running text.

  • Search for ^(Form) (".*")[:space:]*$
  • Replace with $2 $1\n

The leading ^in the search term means "match only from start of new paragraph", and [:space:]* allows for trailing space(s) (but nothing else) before $ dictates "match to end of paragraph". The trailing space matcher is outside parentheses, so the replace will discard them.

This will catch the examples you have given, occurring as solitary items on a line, but will avoid altering running text with constructs such as ... sometimes we may encounter form "misbehavior" in our project ... .

edit flag offensive delete link more



At first it didn't work but I am using:

Search - (Form) (".*") Replace - $2 $1

Which works fine, as I have some terms not in the beginning.

Thank you for your wisdom, very much appreciated...

Chuck D. gravatar imageChuck D. ( 2019-11-08 21:31:24 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-11-08 03:34:15 +0200

Seen: 47 times

Last updated: Nov 08 '19