I hope I'm just missing the obvious here, but how in the world do I match zero or more spaces with Microsoft Word 2010's "Regex" engine?

As a silly example, I want to match all of the following in a capture group:

cowseat grass
cows eat grass
cows  eat grass
cows   eat grass
cows    eat grass

I would normally do (cows\s*eat grass) and be done with it. But I can't see how to match zero or more spaces. I want to capture the whole phrase in a capture group, but I have variable number of spaces.

I've been using this document as a reference.

  • 1
    How is the expression (cows\s*eat grass) behaving? That should, as desired, match zero or more whitespace characters. – Kyle Strand Jan 13 '14 at 20:37
  • @KyleStrand Hi Kyle. The \s doesn't work for whitespace, so I used ` ` instead. The problem is I couldn't get * to work as "zero or more of the preceding character." I could use {1,}, but that doesn't handle when there are no spaces. Maybe I have a wrong setting in Microsoft Word? The Regex engine is very different from what I'm used to (PCRE). – kmort Jan 13 '14 at 20:41
  • yes, I can replicate the problem too. Can't make it match when there are no spaces – Rodolfo Jan 13 '14 at 20:44
  • I don't have Word 2010, so I guess I'll just assume Microsoft fails at regex and call it a day. Sorry. – Kyle Strand Jan 13 '14 at 20:48
  • 2
    yes, according to this article word.mvps.org/faqs/general/usingwildcards.htm it looks like Microsoft in its infinite wisdom decided we don't need a 'zero or more' option – Rodolfo Jan 13 '14 at 20:50

The document you linked to shows that Microsoft's "regular expressions" aren't really regular expressions at all; they're a bizarre hybrid (bastard child, rather) of shell-style globbing (http://www.tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm) and true regular expressions.

Since the glob syntax makes use of the * character as a synonym for the regex .*, and Microsoft decided (as mentioned in a comment) to make @ equivalent to the regex quantifier + instead of * (which is stupid since a+ is equivalent to aa* for any atom a, making + unnecessary), it looks like you're out of luck.

My personal opinion is that (1) this is stupid and (2) calling these patterns "regular expressions" is misleading at best, but unfortunately I don't see any way around this except for abandoning Word in favor of a tool that properly supports regex. (Though I suppose in theory you could try to parse the xml-ish format of the docx file itself, extract the text, and then apply your regex....)

  • 1
    So I just ran a "RegEx" without spaces and one with spaces. It introduced some silliness into the document, but I can fix those by hand. This is a huge gap in functionality in my opinion. Even more bizarre, Visual Studio has it's own Posix-y Regex's, but it's a lot more useble. They could have used that engine for Word. Ugh. Anyway, I'm gonna leave the question open for a few more days to see if anyone comes up with some clever workaround. Thanks Kyle. – kmort Jan 13 '14 at 21:06
  • 1
    @kmort - I am pretty sure it cannot be done. You can search for whitespace in "non-wildcard" mode using ^w, but then you cannot do grouping. Typically, this search would have to be done in VBA with multiple searches, or perhaps with VBA + VBScript's more powerful RegExp object (but then there are other undesirable consequences). Word's wildcard feature hasn't changed much since Word 6, around 1992 - I would guess it was based on one of the simple Posix regex standards of the time. – user181946 Jan 14 '14 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.