Yes, order does matter in a Regex. And it can be of use.
For instance, I have a program that wants to find words in some text, and
match them against a word list. The only snag is, I don't want to match any
that are enclosed in square brackets. So, instead of just having the regex
"\w+"
I have
"\[\s*\w+\s*]|\w+" ( end square brackets ']' don't need escaping)
That way, if it can enclose the word in square brackets, it will. And since
the square brackets aren't included in the words in the list I'm checking
against, in that case it won't match. But if there is a word that isn't in
square brackets, then it will match the bit to the right of the pipe, which
has a chance of being in the word list. But I know that if it matches the
"\w+" bit, then it obviously didn't match the word-in-square-brackets bit,
and thus hasn't got square brackets round it.
"Ryan Taylor" wrote:
Hello.
I am trying to create a regular expression that will let me know if a string
has the following criteria. Order does not matter in the string, but when
building a regular expression it does, right? I am brand new to regular
expressions and having a little (read: a lot of) difficulty in making a
regular expression that makes sure the string meets the following rules.
The string contains at least one character from two of the following four
sets.
- an uppercase alpha character
- a lowercase alpha character
- a digit
- a punctuation mark (IE: , . ; ' " ! ?)
The problem that I am having is that order matters in the regular expression
but it does not matter in my string. Any thoughts or references that might
help me out a little?
Thanks in advance for you help.
Ryan Taylor