What are Regular Expressions?

I l @ ve RuBoard

One chapter on regular expressions can cover only the tip of the iceberg, yet it should be enough to get you on your way. Should you take to regular expressions and desire to learn more, Appendix C, PHP Resources, will lead you to deeper waters.

Think of regular expressions as an elaborate system of matching patterns. You first write the pattern, then use one of PHP's built-in functions to apply the pattern to a text string (regular expressions are specifically for use with strings). PHP has essentially two functions for using regular expressions to match patterns (one case sensitive and one not) and two for matching patterns and replacing matched text with other text (again, one case sensitive and one not).

I'll begin with how to define and match a simple pattern, then I'll delve into more complex patterns and I'll finish with how to match and replace patterns.

Tip

Some text editors, such as BBEdit for Macintosh, TextPad for Windows and emacs for Unix, allow you to use regular expressions to match and replace patterns within and throughout several documents (Figure 8.1). This may be another good reason to learn regular expressions and is perhaps something to consider when choosing your text editor.

Figure 8.1. Within BBEdit's standard Find dialog box, you have the option of using regular expressions (even across several files or folders) by checking the Use Grep box. BBEdit can also store regular expression patterns for you and has some built-in.

graphics/08fig01.jpg


Tip

The PHP manual examines the differences between how PHP and Perl treat regular expressions (see Perl-compatible Regular Expression Functions under section four, Function Reference ). For those of you who are coming to PHP from the latter, you may want to read the manual pages first.


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net