regular expressions. Order matters?

  • Thread starter Thread starter Ryan Taylor
  • Start date Start date
R

Ryan Taylor

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
 
sure, apply four different regular expressions. If more than two of them
match, you are good to go.

Don't create an overly complex regex for a password algorithm.

--- Nick
 
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.
 
I have made a number of smaller regular expressions and checked them each
individually to ensure that the string passes the rule check. This was
definately much easier than trying to create a super expression that would
check everything at once.

Thanks again.
Ryan Taylor
 
Back
Top