h1

Word: Find and replace multiple spaces after a punctuation mark

January 4, 2012

Your document has varying numbers of spaces after a punctuation mark — for example, 2, 3, 4 etc. spaces after a semicolon, colon, period, comma etc. You want to be able to find all multiple spaces after a defined punctuation character and replace them with a single space in one Find/Replace action.

Here’s how to do it for a semicolon. Use the same process for other punctuation marks, substituting your punctuation character where I’ve used a semicolon.

  1. Open Word’s Find and Replace dialog box (Ctrl+H).
  2. Click More to show more options.

    Find and Replace dialog - click the More button

    Click More

  3. Select the Use wildcards check box.

    Find and Replace dialog - select Wse Wildcards

    Select Use Wildcards

  4. In the Find what field, type: (;)( {2,9})
    Note: There’s ONE space in this text — it’s between the second ( and the {. To be safe, copy the text from Step 4 and paste it into your Find what field.
  5. In the Replace with field, type: \1
    Note: There’s ONE space immediately after the \1, so make sure you type that too.

    Find and replace dialog - make sure you enter the spaces

    Make sure you enter the spaces in the correct places

  6. Click Find Next to find the next instance of a semicolon followed by more than one space. If the Find is successful and you are confident you’re not going to mess anything up, click Replace All. If you want to check each instance before replacing it, just click Replace then Find Next, Replace until you’ve dealt with them all.

Explanation for how this works:

  • (;) looks for the semicolon. Because you are using wildcards, you need to surround the text you want to find in parentheses.
  • ( {2-9}) looks for a space followed by two or more spaces, up to 9 spaces (you can put whatever numbers you like inside the curly braces — if you think you might have some instances of semicolons followed by 15 spaces, then change these numbers to {2-20}, for example. Again, this section is surrounded by parentheses to define it as a separate section.
  • \1 replaces the first part of the wildcard string with itself. In other words, the semicolon is replaced with itself, so no change apparently occurs.
  • The <space> after \1 replaces the multiple spaces found in the second part of the wildcard string with a single space.

You can use this same technique for multiple spaces after any other punctuation character — just replace the semicolon with a comma, colon, period, etc.

NOTE: Some characters — [ \ ^ $ . | ? * + ( ) — are special and need to be ‘escaped’ before they can be replaced. To ‘escape’ a special character, type \ in front of it. For example, to run the same find/replace above for a question mark, you’d type this into the Find what field: (\?)( {2,9})

See also:

[Links last checked December 2011]

13 comments

  1. [...] newsletter/blog of CyberText Consulting – technical communication specialists « Word: Find and replace multiple spaces after a punctuation mark Word: Find and replace multiple spaces before a number January 5, 2012 Your document has [...]


  2. [...] Word: Find and replace multiple spaces after a punctuation mark [...]


  3. [...] [...]      by Word: Find and replace multiple spaces after a punctuation mark « CyberText Newsletter January 4, 2012 at 6:36 [...]


  4. [...] Bracey explains how to find and replace multiple spaces after a punctuation mark in [...]


  5. Just curious if there was anyway to expand off of this to force the next letter following these corrections to be capitalized? To force the correct spacing that you want and then to take it one step further to force capitalization.


  6. Hi Matthew

    Not that I could find. I did a fair bit of testing, but couldn’t figure out how to do this using wildcards.

    However, there’s a macro you can use for that second step of changing a lower case letter after a period and space to a capital. I’ll be writing this up as a separate blog post to be published some time next week.

    Meantime, be sure that this is what you want to do. I suggest you think about it before trying it (make a backup of your document!), as it should convert ANY letter after your punctuation mark to a capital letter. While that may be fine in most circumstances, you’ll need to watch for things like ‘e.g.’, ‘i.e.’, ‘etc.’, and any abbreviation/acronym that has periods, for example. Make sure that’s what you want.

    If you’re not already subscribed to this blog, click the ‘Sign me up!’ on the right to get a notification each time I add a new blog post. That way, you won’t miss the one that has the macro.

    –Rhonda


  7. Some good commercial tools are available which help in Find and replace words in Multiple files.

    http://www.lantechsoft.com/advance-find-replace.html


  8. [...] lower case letter after a period using Find/Replace (see Matthew’s 25 January 2012 comment on this blog post). My wildcard skills just aren’t sophisticated [...]


  9. Thanks for the reply! It was useful info, but there are a myriad of things I’ll have to be cognizant of such as e.g., etc., Dr., and many others.

    This might sound like an odd question and you don’t have to answer it if you know how, but a yes/no would be helpful for me.

    Is there a way that you can Find Replace multiple words in a document and replace with a single word? Lets say my company used different abbreviations throughout the years for a single word. Now that I have to update that document I need to look for ABBV1, ABBV2, ABBV3, and replace them all with ABBV4CUZITSSOMUCHBETTER! :)

    I’ve set up a macro via Macro Express Pro that uses keystrokes to run the Find Replace window for me to check the document but it’s running at a couple a minutes for just 7 words (as there are many variations and misspellings of each abbreviation). I was hoping to capitalize by combining all of my variations (one has 7 variations alone), and replace with the single new abbreviation. I hope that makes sense.

    If it can be done I’ll keep digging away, and if not then I’ll stop banging my head against the keyboard!

    Thanks again,

    Matthew


  10. That’s a tough one, Matthew. Short of running multiple find/replaces, I can’t think of an easy way to do this. You might be able to use a combination of wildcards, but I don’t know what — check the Microsoft Help for details and examples of wildcard use. Also, check out EditTools, as they have a wildcard find/replace function that goes beyond what Word can do, including a multifile find/replace: http://wordsnsync.com/edittools.php
    Also, check out PerfectIt (http://intelligentediting.com/) as it may alert you to variations and allow you to change them to a single term/phrase.

    Both these have a free trial option, so it may be worth checking them out.

    –Rhonda


  11. OK. Here is what I’ve done. I created this in Macro Express Pro, which took minutes to run as it was actually calling on the find replace.

    I think wrote it in VBA for word and it does it all in the blink of an eye. For this particular article I set it up like this:

    Sub Punction Replacement()

    ‘ Question Mark Replacement

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = “(\?)( {2,9})”
    .Replacement.Text = “\1 ”
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    End Sub

    I then just did the same for periods. I set up a second one to look specifically for those abbreviated words that I was stuck on. It will replace all instances and fix all spacing issues within a second for the whole document.

    You helped me and maybe this will help others?


  12. Thanks Matthew — and congratulations on finding a solution to your problem, then contributing it here.

    –Rhonda



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 345 other followers

%d bloggers like this: