Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Regex-tut] Multiples
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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:

code:
/([hH]ello|[tT]oodles), world/


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.

code:
/([hH]ello|[tT]oodles),? world/


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:

code:
/([hH]ello|[tT]oodles),?\sworld/


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.

code:
/([hH]ello|[tT]oodles),?\s+world/


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:

code:
/^([hH]ello|[tT]oodles),?\s+world$/


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.

code:
/^\s*([hH]ello|[tT]oodles),?\s+world\s*$/
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: