Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Regular Expression Syntax Examples

Table 15-6 is a comprehensive table of all the regular expressions we've covered. Column one contains example expressions, and column two contains what each expression will match.

Table 15-6. Complete list of regular expression examples

Expression

Will match . . .

foo

The string "foo"

^foo

"foo" at the start of a line

foo$

"foo" at the end of a line

^foo$

"foo" when it is alone on a line

[Ff]oo

"Foo" or "foo"

[abc]

a, b, or c

[^abc]

d, e, f, g, V, %, ~, 5, etc.—everything that is not a, b, or c (^ is "not" inside character classes)

[A-Z]

Any uppercase letter

[a-z]

Any lowercase letter

[A-Za-z]

Any letter

[A-Za-z0-9]

Any letter or number

[A-Z]+

One or more uppercase letters

[A-Z]*

Zero or more uppercase letters

[A-Z]?

Zero or one uppercase letters

[A-Z]{3}

Three uppercase letters

[A-Z]{3,}

A minimum of three uppercase letters

[A-Z]{1,3}

One, two, or three uppercase letters

[^0-9]

Any non-numeric character

[^0-9A-Za-z]

Any symbol (not a number or a letter)

(cat|sat)

Matches either "cat" or "sat"

([A-Z]{3}|[0-9]{4})

Matches three letters or four numbers

Fo*

F, Fo, Foo, Fooo, Foooo, etc.

Fo+

Fo, Foo, Fooo, Foooo, etc.

Fo?

F, Fo

.

Any character except \n (new line)

\b

A word boundary; e.g. te\b matches the "te" in "late" but not the "te" in "tell."

\B

A non-word boundary; "te\B" matches the "te" in "tell" but not the "te" in "late."

\n

Newline character

\s

Any whitespace (new line, space, tab, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page