Regular Expressions: An Introduction
Published: January 12, 2004
User Rating: 9.0 (281 votes)
Alternates
Alternate, as the name suggest allows you to define an alternate in the patter. Or in simple terms it would mean "find this or that or ....".
Alternate or alternates are defined by seperating them with a pipe | character. This would become more clear from taking a few examples.
Regex /cat|rat|bat/
Explaintaion Match "cat" or "rat" or "bat"
Example cats True
brat True
batting True
Regex /(c|b|d)are/
Explaintaion Match "c" or "b" or "d", followed by are
Example care True
bare True
dare True
Note Notice that the c, b, d are enclosed in the brackets. This is called a sub-pattern
Sub-patterns are patterns within pattern. We 'll discuss them later, but I just wanted to mention them here so that there is no confusion
Taking few more examples ...
Regex /(my|your)\s(c|b|h)at/
Explaintaion Match "my" or "your", then a space, then "c" or "b" or "h", then "at"
Example "This is my cat" True
"Where is your bat ?" True
"Is that my hat ?" True
About
Amit Arora is web developer with expertise in developing eCommerce enabled websites for the businesses.

Monitored by Site24x7
Uptime