Computer Science Canada [Regex-tut] Multiples |
Author: | wtd [ Sat Nov 13, 2004 9:46 pm ] | ||||||||||||
Post subject: | [Regex-tut] Multiples | ||||||||||||
It's either there or not In the last tutorial I spoke of alternatives, groups and sets. The last regex used was:
Now, what if I want to either recognize that comma or not? This is a multiples situation. I want either one comma or none. The ? suffix provides this.
Once, and maybe more... oh, and something else Now, you'll also notice that I put one space between the greeting/parting and world. What difference does it make to me if someone uses one or more spaces? Maybe they used a tab. It's all the same to me. The \s entity respresents any whitespace, so a simple rewrite looks like:
Now, I still have the problem of wanting to match the string even if someone has used more than one space. The + suffix handles this nicely.
Maybe nobody's home, or maybe there's a party going on I mentioned in a previous tutorial that ^ and $ are sued to represent the beginning and end of the string. So let's say I want to include those:
Of course, now I can't have anything before the greeting/parting or after world. But that's what I wat, right? Yes. On the other hand, what do I care if the stuff before or after the expression I'm looking for is just empty space? I want to look for any number of spaces, and I also want to be able to match no spaces. The * suffix will handle this for me.
|