PHP

Boundaries

One of the things shown previously was that searching for pants matched both pants and underpants. When we want to match only the word pants, we need a way to mark word boundaries. This is done in POSIX regular expressions by using the [:<:] and [:>:] anchors, for a word's left and right boundaries, respectively. As with other special classes listed in Table 1 these must be used within character classes. These two anchors are used in regular character classes as follows:

the_regex($clothes, '[[:<:]]pants[[:>:]]');

The beginning of a string and the end of a string count as a left and right word boundary, respectively, so the preceding code would generate the following results:

the_regex called to match '[[:<:]]pants[[:>:]]':
Array Index 1 matches: array ( 0 => 'pants', ) "pants"

Note

Recent PHP Versions as 5.0.4 have an issue when compiled for the Microsoft Windows platform. On these, if you have php_mbstring.dll enabled, you cannot use the word boundary anchors [:<:] and [:>:] in regular expressionsthey generate a regular expression compiler error. Unix versions of PHP do not have this problem, and neither do Windows versions without mbstring enabled.

Two other important anchors exist for matching the beginning of a string (the caret, or ^) and the end of a string (dollar sign, or $). These are used on their own, outside of character classes wrapped in [ and ], and are known as metacharacters.

To match any string beginning with the word the, we could use the regular expression "^the". If we want to allow either a lower- or uppercase t character, we rewrite the expression as "^[tT]he". In the clothing example, to find words starting with the letter s, we would write the following:

the_regex($clothes, '^s');

The output would be as follows:

the_regex called to match '^s':
Array Index 0 matches: array ( 0 => 's', ) "shoes"
Array Index 2 matches: array ( 0 => 's', ) "socks"
Array Index 5 matches: array ( 0 => 's', ) "scarf"

Similarly, to find an article of clothing ending with the letter s, we would write this:

the_regex($clothes, 's$');

And our output would be this:

the_regex called to match 's$':
Array Index 0 matches: array ( 0 => 's', ) "shoes"
Array Index 1 matches: array ( 0 => 's', ) "pants"
Array Index 2 matches: array ( 0 => 's', ) "socks"
Array Index 8 matches: array ( 0 => 's', ) "underpants"

The regular expression "^s$" matches strings containing only the letter s.

by BrainBellupdated
Advertisement: