2

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.

5
  • This may be a better question for StackOverflow.com instead (which is about programming). What language are you using, and can you post the replace code in your question? Dec 4, 2013 at 15:48
  • 1
    Are those (faulty) postal codes always exactly six characters long? Are they in a new line every time?
    – nixda
    Dec 4, 2013 at 15:50
  • Not programming- just using MS Word 2003 on a document with about 30 pages of address labels-
    – Mark
    Dec 4, 2013 at 16:08
  • ie: Mr. John Doe 123 Any St. Toronto, ON M2N3X5
    – Mark
    Dec 4, 2013 at 16:10
  • Yes, postal codes always 6 characters long - or I guess 7 with the space. They are usually on the same line as the city, Province
    – Mark
    Dec 4, 2013 at 16:12

3 Answers 3

2

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)

1
  • Or, more simply, replace ([A-Z][0-9][A-Z])([0-9][A-Z][0-9]) with \1 \2. (And, D’oh! I don’t know why I didn’t find that.) Mar 1, 2014 at 21:41
0

I can’t find any way (in Microsoft Word’s “Find and Replace” dialog) to do full sub-regex replacement, the way you can in programs like sed.  But I did find a multi-step solution to your problem.  First, identify a string that does not appear in your data and does not contain any characters that are special in Word’s “Find and Replace” (e.g., ‘@’ or ‘#’).  (Alternatively, use them but escape them.)  It can be a single character, like ‘|’, so long as it doesn’t appear in your data.  I’ll assume that you’ve chosen “|”.  Now,

  1. Do a search for [A-Z][0-9][A-Z][0-9][A-Z][0-9] and replace with ^&|.  This will tag each non-conformant postal code with a trailing “|”.
  2. Search for [0-9][A-Z][0-9]| and replace with  ^& (with a leading space).  This inserts the space where you want it.
  3. Then search for all occurrences of | and replace with nothing.

You can do this without using wildcard mode:

  1. ^$^#^$^#^$^#^&|.
  2. ^#^$^#| ^&                (or even ^?^?^?| ^&).
  3. | → nothing.

Note that ^$ is equivalent to [A-Za-z], so if your situation is case-sensitive, I guess you need to use the “wildcards”.

2
  • Excellent- don't know what sub-regex means I'll have to look it up- but this worked for me! Thank you
    – Mark
    Dec 4, 2013 at 16:53
  • It’s just a term I made up to refer to using codes like \1 to include a substring of what you found (using a regular expression) in the replacement. (In contrast to ^&, which Word uses to include the entire matched string.) Dec 4, 2013 at 16:56
0

Super simple way: use the "Text to Columns" function in Excel and then use the CONCATENATE formula to recombine with a space in the middle. The formula would be =CONCATENATE(A1," ",B1)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .