Escape Sequences
There are three ways to match a single character that could be one of several characters. The first way involves using the . meta-character, which will match any single character except a line feed (“\n") without use of modifiers (which will be covered later). This can be used with repetition just like any other character. The second way requires using special escape sequences that represent a range of characters. Aside from the escape sequences mentioned in the previous section’s examples, here are some that are commonly used.
Each of these escape sequences has a complement.
The third and final way involves using character ranges, which are characters within square brackets ([ and ]). A character range represents a single character, but like normal single characters they can have repetition applied to them. <?php // Matches the same as 1d $matches = (preg_match('/[0-9]/', $string) == 1); // Matches the same as W $matches = (preg_match('/[a-zA-Z0-9_]/', $string) == 1); ?> Ranges are respective to ASCII (American Standard Code for Information Interchange). In other words, the ASCII value for the beginning character must precede the ASCII value for the ending character. Otherwise, the warning “Warning: preg_match(): Compilation failed: range out of order in character class at offset n” is emitted, where n is character offset within the regular expression. Within square brackets, single characters and special ranges are simply listed side by side with no delimiter, as shown in the second example above. Additionally, the escape sequences mentioned earlier such as \w can be used both inside and outside square brackets.
There are two other noteworthy points about character ranges, as illustrated in the examples below. <?php // Using a literal ] in a character range is done like so $matches = (preg_match('/[\]]/', $string) == 1); // Matches any character that is not 'a' $matches = (preg_match('/[~a]/', $string) == 1); // Using a literal ^ in a character range is done like so $matches = (preg_match('/[\~]/', $string) == 1); $matches = (preg_match('/[a~]/', $string) == 1); ?>
© PCRE Extension — Web Scraping >>> Back to TABLE OF CONTENTS <<< | |
Views: 385 | |
Total comments: 0 | |