Perl has long been considered one of the most powerful parsing languages ever written, and it provides a comprehensive regular expression language that can be used to search and replace even the most complicated of string patterns. The developers of PHP felt that instead of reinventing the regular expression wheel, so to speak, they should make the famed Perl regular expression syntax available to PHP users. Perl’s regular expression syntax is actually a derivation of the POSIX implementation, resulting in considerable similarities between the two. You can use any of the quantifiers introduced in the previous POSIX section. The remainder of this section is devoted to a brief introduction of Perl regular expression syntax. Let’s start with a simple example of a Perl-based regular expression: /food/ Notice that the stringfoodis enclosed between two forward slashes. Just as with POSIX regular expressions, you can build a more complex string through the use of quantifiers: /fo+/ This will matchfofollowed by one or more characters. Some potential matches includefood,fool, andfo4. Here is another example of using a quantifier: /fo{2,4}/ This matchesffollowed by two to four occurrences ofo. Some potential matches includefool,fooool, andfoosball. Modifiers Often you’ll want to tweak the interpretation of a regular expression; for example, you may want to tell the regular expression to execute a case-insensitive search or to ignore comments embedded within its syntax. These tweaks are known as modifiers, and they go a long way toward helping you to write short and concise expressions. A few of the more interesting modifiers are outlined in Table 9-1. Table 9-1. Six Sample Modifiers
These modifiers are placed directly after the regular expression—for instance,/string/i. Let’s consider a few examples:
Metacharacters Perl regular expressions also employ metacharacters to further filter their searches. A metacharacter is simply an alphabetical character preceded by a backslash that symbolizes special meaning. A list of useful metacharacters follows:
Let’s consider a few examples. The first regular expression will match strings such aspisaand lisabut notsand: /sa\b/ The next returns the first case-insensitive occurrence of the wordlinux: /\blinux\b/i The opposite of the word boundary metacharacter is\B, matching on anything but a word boundary. Therefore this example will match strings such assandandSallybut notMelissa: /sa\B/ The final example returns all instances of strings matching a dollar sign followed by one or more digits: /\$\d+\g Please check back next week for the continuation of this article.
blog comments powered by Disqus |
|
|
|
|
|
|
|