Computer Science Canada [Regex-tut] Alternatives, Groups, and Sets... oh my! |
Author: | wtd [ Sat Nov 13, 2004 9:18 pm ] | ||||||||
Post subject: | [Regex-tut] Alternatives, Groups, and Sets... oh my! | ||||||||
Alternatives Alternatives let us create regular expressions that match one thing or another... or another, or another. Let's say we want to match a string that contains a greeting for world, regardless of whether the greeting is hello or goodbye. No exclamation point this time, because I'm not feeling as enthusiastic about this "world" guy.
Groups Now, we should look at that previous regex:
I've talked about the word "pattern", and you should notice a pattern here. The only thing that changes is the greeting (or parting) word used. We can use a group to make the "|" work on only part of the expression.
Much better. This is beginning to look like a pattern. Sets Now, why am I being pedantic about use of case? Shouldn't it be just as friendly if someone used Hello or Toodles without the capital letters? A set matches any one of the characters contained within it. So we can use sets comprised of "h", "H", "t" and "T" to do the trick.
|