Tuesday, September 11, 2018

regex - Word find/replace: add space to postal code with wildcards


Using the “Find and Replace” dialog in Microsoft Word 2003,
I want to search a list of addresses for postal codes (Canadian) that do not have a space in them, e.g.,


M2N3X6
L4C2A9
K5G1S7

and put a space in the middle:


M2N 3X6
L4C 2A9
K5G 1S7

This should be simple, but I am having a bit of trouble with the "replace with" part:
I've figured out that in the Find field I enter [A-Z][0-9][A-Z][0-9][A-Z][0-9],
but in the Replace box, how do I add the space in the middle, retaining the characters that are there?
I thought it would be something like \1\2\3 \4\5\6, but that's not working


Any help appreciated.


Answer



The \1 etc. replace expressions, and in order to specify that something is an expression, you enclose it in (). So you need


([A-Z])([0-9])([A-Z])([0-9])([A-Z])([0-9])

Then


\1\2\3 \4\5\6

should work. I think there is a limit on the number of expressions you can have (9, perhaps)


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...