Subexpressions (also called subpatters or atoms) are are smaller patterns enclosed in parenthesis that are treated as a single unit. For example, if you used cat{3}, it would match cattt but if you used (cat){3}, it would match catcatcat. The example below will use subexpressions to see if the input looks like a zip code. A zip code can be five numbers or five numbers followed by a hyphen followed by four more numbers. The regex I use for that is
ereg("^[0-9]{5}(\-[0-9]{4})?$", $in)
| Analysis: | |
|---|---|
| [0-9] | Any number from 0 to 9 |
| [0-9]{5} | Five numbers from 0 to 9 |
| (\-[0-9]{4}) | An atom |
| (\-[0-9]{4})? | An optional atom |
| \- | A literal hyphen (Hyphens must be escaped if they are part of the pattern) |
| [0-9]{4} | Four numbers from 0 to 9 |
| ^ | An anchor. Indicates that there should be nothing before the match |
| $ | An anchor. Indicates that there should be nothing after the match |
As mentioned above, ^ and $ are called anchors. When used outside of a character set, it indicates that there should be nothing before the pattern that you are looking for. (Used inside of a character set, ^ is used to negate the set.) In addition, you can match word boundries with \b. If you were to search for the string, cat within the string concatenate, cat, cataloge, you'ld get three matches"
concatenate, cat, cataloge
to match the word cat, you'ld use \bcat\b. What \b does is match the boundry between a character matched by a character matched by \w and a character matched by \W
If you want to have a brance of choices to match, separate them with the pipe character | . For example. my image tool will only work on images. To check the ending of the input, I use:
{ if ( !preg_match("#php|jpg|jpeg|png|jpe|gif$#i", ) )
{ die(" Error Message ") }
The only input the form will accept is a string that ends in php, jpg, jpeg, png, jpe, or gif
|
|
|