Find/Replace in MS Word is a powerful tool, but many people don’t realize just how powerful it can be.

I had a list of dates I wanted to put quotes around.  Here is what I wanted to happen:

transform1.jpg

I could have done it one by one, but I am lazy.  Not only am I lazy, there is a shortcut you can use — so why waste time?  With a little bit of regular expression magic, you can do this in about 10 seconds. What are regular expressions? Well . . .


For the uninitiated, regular expressions is tech-speak for pattern matching.  Have you ever had to add some text to a long list of items?  You could do it manually, one list item at a time, or you could use regular expressions and do it all in one go.

more.jpg
wildcards.jpg

To start, I go to Edit > Replace.

I click the “More” button.

Finally, I check “Use wildcards”

For more on wildcards, trust wikipedia’s entry (because it’s written by NERDS!).  The summary is, a wildcard is a single character (like *) used to represent something.  * happens to represent ANYTHING.  So if you try and Find: *, you will match every single character in your document.

Now to write the Find text.  I’ll begin with the end, and break it down for you.  The final expression I used is ([0-9]{1,2}/[0-9]{1,2}/[0-9]{4}).

final.jpg

Now, if you’re not already familiar with regular expressions, that looks like a whole bunch of garbage.  What I am trying to do here is find ANY date in the MM/DD/YYYY format.

Let’s start with the first and the last characters — “(” and “)”.  Surrounding anything with parenthesis is a way to say, “Save this value so I can use it later.”  So if I had said Find: (Abc), Abc would be saved.  (Abc) is stored in 1.  You can put 1 in the “Replace with:” field, and it will find Abc into your document.  This is not useful unless you want to add something to the end or beginning of a word.  So for example, if I asked MS Word to replace (Abc) with 1d, Abc would change to Abcd.  ANY Abc in the document will change into Abcd after you run this Find/Replace.  Sounds weird, so let’s take a look:

explain-backslash.jpg

So again, my full expression is ([0-9]{1,2}/[0-9]{1,2}/[0-9]{4}).  We’ve covered the parenthesis, so I’ve made them bold.

Next, let’s look at the “[0-9]{4}”.  “0-9″ means, “find me any number between 0 and 9″, or, “find 0,1,2,3,4,5,6,7,8 or 9″.  The {4} means, “look for a number between 0 and 9 four times.”  If I hadn’t used the Brackets, and said 0-9{4}, it would have looked for “9999”.  So brackets [] are used to define groups of things you want to search for.

[0-9]{4} will match 1234, 2008, 3577, 9876, 9999 — any series of four numbers.

([0-9]{1,2}/[0-9]{1,2}/[0-9]{4}). So now let’s look at the slashes /.  There is a slash before the [0-9]{4}.  This means we’re searching for a slash, followed by four numbers.  Can you tell yet that I’m looking for a date?

What about [0-9]{1,2}?  Well a{4} means, “find ‘aaaa'”  but a{1,2} means, “Find ‘a’ or ‘aa.'”  Normally, using regular expressions we’d just say a+.  Which means, “Find ‘a’ one or more times.”  But, as noted here, MS Word does not have an equivalent to the “+” regular expression.  So the have this wacky comma thing.

So, the full string, ([0-9]{1,2}/[0-9]{1,2}/[0-9]{4}) basically means, find me any date in the format, MM/DD/YYYY.

Now that I have my dates, I want to put quotes around them.  So, in the “replace” field, I simply say “1”.  This will put quotes around the complete date I have found.

Hit “Replace All” and all your dates will now have quotes around them.



One Response to “Quickly edit text in a list of items in MS Word (or, an intro to regular expressions)”  

  1. 1 sharon

    livetotry.com is written by nerds!

    i’m calling you next time i have a really crazy Find/Replace situation on my hands

Leave a Reply