Paris Char
Paris Char

Reputation: 497

Capture groups in MS Word regex

I am trying to remove the new line prior to "n=", replace with a space and contain the captured number in (), all these in MS Word's advanced find+replace, using wildcards.

Currently:

some preceeding text
n=1,233,023

Desired result:

some preceding text (1,233,023)

I've been struggling with ^13n=(*{1,}) and replace with " (\1)" (without the quotes) but it can't even match it. Any help please , appreciated. Thank you

Upvotes: 4

Views: 2607

Answers (2)

freeflow
freeflow

Reputation: 4355

The wildcard search term should be

(^13)([a-z])(=)([,0-9]{1,})

and the replacement is

(\4)  

Note the first character above is a space.

Upvotes: 2

JvdV
JvdV

Reputation: 75840

MS Word does have weird ways in regular expressions. The following steps were succesfull for me (mine is in Dutch so please forgive any small translations errors):

  • Hit Ctrl+H to open Search And Replace.
  • Tick More and tick Use Wildcards

Now with this done we can search for:

^13(n=[0-9,]{1,})
  • ^13 - Match newline.
  • ( - Open capture group 1.
    • n= - Match "n=" literally.
    • [,0-9]{1,} - Match a digit or commas at least 1 time.
  • ) - Close capture group 1.

Replace by:

^s\1
  • ^s\1 - A space followed by capture group 1.

As mentioned I would consider the type of regular expressions Word is offering dodgy. Here you can read a bit more about it's flaws too. I couldn't create capture groups within a capture group neither was I able to create optional blocks of three consecutive digits and commas. Fortunately in your own attempt just knowing a newline followed by literally n= seemed enough.

Second to last note; because I'm Dutch my local parameter seperator is the semi-colon. This also reflects in this search and replace function its occurrence indicators meaning I used: ^13(n=[,0-9]{1;})

And one last note, another pattern I found worked for me was ^13(n=*^13), but since we had zero control of the pattern between n= and the paragraph end I would stick with my initial thought. The reason why the use of the * worked here is because we used it as an actual frequence of any characters between n= and ^13.


Before:

enter image description here

After:

enter image description here

Upvotes: 8

Related Questions