RaifMT
RaifMT

Reputation: 95

Regular Expression in Word VBA

I have the following regular expression syntax in VBA for MS Word which works:

regEx.Pattern = "\w*\(\w*\s[0-9]{1,3}:[0-9]{1,3}(\)|\-[0-9]{1,3}\))"
MsgBox regEx.test("This is a test (Chronicles 13:12-14)")

This returns True, as desired. I encounter difficulty when I attempt to include patterns for "First", "Second", "Third", 1, 2, 3, or nothing immediately following my opening parenthesis. I have tried [First|Second|Third|1|2|3]? but that returns false when I add any of those strings/numbers inside the parenthesis. What am I missing?

Thanks for the explanations and suggestion. I added a final "|" without anything after it to account for the lack of any of the numbers. I also had to add spaces in each option. The final working version is "\w*\((First |Second |Third |[1-3] |)\w*\s[0-9]{1,3}:[0-9]{1,3}(\)|\-[0-9]{1,3}\))"

Upvotes: 0

Views: 163

Answers (2)

Kasravnd
Kasravnd

Reputation: 107347

If you wan to use OR operand you shouldn't put your words within a character class. instead you can use a capture group :

(First|Second|Third|1|2|3)

Because inside a character class the regex engine assume your pattern as any combination between the characters of your words and the character |.

For better understanding see the following diagrams :

[First|Second|Third|1|2|3]

Regular expression visualization

Debuggex Demo


(First|Second|Third|1|2|3)

Regular expression visualization

Debuggex Demo

Upvotes: 1

Sam
Sam

Reputation: 20486

Character classes ([]) match literally any of the contained characters. Because of that, [First|Second|Third|1|2|3] matches F, i, r, s, t, |, S, etc.

You'll want to use alternation outside of the character class. You can group this using (...) or if you want a non-capturing group (?:...).

Try replacing [First|Second|Third|1|2|3] with (?:First|Second|Third|[1-3]).

Upvotes: 1

Related Questions