Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Regular expressions Wish List | Report Bug | About Me ]

4.8.6     Regular expression syntax examples

This is NOT the latest copy of this book; click here for the latest version.

In order to give you a quick reference to the different patterns and what they will match, here's a comprehensive table of all we've covered. Column one contains example expressions, and column two contains what that expression will match.

Expr

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, h, etc - everything that is not a, b, or c (^ is "not" inside sets)

[A-Z]

any uppercase letter

[a-z]

any lowercase letter

[A-Za-z]

any letter

[A-Za-z0-9]

any letter of 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}

3 uppercase letters

[A-Z]{3,}

a minimum of 3 uppercase letters

[A-Z]{1,3}

3 uppercase letters

[^0-9]

any non-numeric character

[^0-9A-Za-z]

any symbol (not a number or a letter)

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

new line character

\s

any whitespace (new line, space, tab, etc)

\S

any non-whitespace character





<< 4.8.5 Regular expression replacements: preg_replace()   4.8.7 The regular expressions coach >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
sandeep_war@rediffmail.com - 29 Aug 2008

I have an expression
((( 5339) * ( 1 + Cess Duty + 0.14 * ( 1 + 0.02 + 0.01) + Packing & Forwarding) + Packing & Forwarding fixed))

In the above expression the value to be replaced is coming from sql query in which "Packing & Forwarding" is coming before "Packing & Forwarding fixed". When i use replace function both "Packing & Forwarding" gets replaced and in second place only "0 fixed" remains. How to replace this value.

Anybody can help me.....

Thanks in advance

Sandeep

amateur - 29 Aug 2008

Excellent! But I dont fully understand the use of the () brackets. =/

amateur - 29 Aug 2008

Excellent! But I dont fully understand the use of the () brackets. =/

A PHP User - 29 Aug 2008

can someone explain me what this expression does exactly

Set your own pattern of preg_replace function.Default: /[,.\-:><()!?\n]/
in case of problems try this: /[^a-zA-Z0-9\200-\377]/

Hmmm - 29 Aug 2008

I want to do this - whenever I get a filename stored in a variable (f.e. $file1), I want to remove all dots from this variable, EXCEPT if they are in last four or five spots (so I dont delete file extension.

Hmmm - 29 Aug 2008

I want to do this - whenever I get a filename stored in a variable (f.e. $file1), I want to remove all dots from this variable, EXCEPT if they are in last four or five spots (so I dont delete file extension.

Hmmm - 29 Aug 2008

I want to do this - whenever I get a filename stored in a variable (f.e. $file1), I want to remove all dots from this variable, EXCEPT if they are in last four or five spots (so I dont delete file extension.

A PHP User - 29 Aug 2008

This page has helped a ton! ...however it doesn't really address () operations...

jmoliveira at ulusofona dot pt - 29 Aug 2008

I'm a newbie (i know more chinese than regexp) but i solved my problem. If it is of any good to any other user, here is the code:

function auto_link($foo) {

$foo = preg_replace('/(?<!\S)([A-Z_\-\w-.]+)(@)([A-Z_\-\w-.]+)\b/i', '<a href="mailto:$1@$3">$1@$3</a>', $foo);
$foo = preg_replace('/(?<!\S)((http(s?):\/\/)|(www\.))+([\w.\/&=?\-~%;]+)\b/i', '<a href="http$3://$4$5" target="_blank">http$3://$4$5</a>', $foo);
$foo = preg_replace('/(?<!\S)((ftp(7?):\/\/)|(ftp\.))([\w.\/&=?\-~%;]+)\b/i', '<a href="ftp$3://$4$5" target="_blank">ftp$3://$4$5</a>', $foo);
return ($foo);
}

I hope my code ain't that bad at all.

Best Regards

jmoliveira at ulusofona dot pt - 29 Aug 2008

message to d at aerno dot com

Hey man, could you help me? your script is the best I found on the web to create auto links from url's and e-mails. Although I have an e-mail with an hyphen (-) and the replace ends there, someting link this:

abc@be-f.gh => abd@be

what can I do to repair this? I'm trying to understand the syntax of regexp but it is going to take a while and my time is running out.

Many thanks in advance

jmoliveira at ulusofona dot pt - 29 Aug 2008

message to d at aerno dot com

Hey man, could you help me? your script is the best I found on the web to create auto links from url's and e-mails. Although I have an e-mail with an hyphen (-) and the replace ends there, someting link this:

abc@be-f.gh => abd@be

what can I do to repair this? I'm trying to understand the syntax of regexp but it is going to take a while and my time is running out.

Many thanks in advance

A |\| 3 \/\/ 13 | E PHP User - 29 Aug 2008

My brain hurts :-\

d at aerno dot com - 29 Aug 2008

This is for the person who wished to see a more practical example of hyperlinking an email address or URL.

function autolink($foo) {
$foo = preg_replace('/(?<!\S)([\w.]+)(@)([\w.]+)\b/i', '<a href="mailto:$1@$3">$1@$3</a>', $foo);
$foo = preg_replace('/(?<!\S)((http(s?):\/\/)|(www\.))+([\w.\/&=?\-~%;]+)\b/i', '<a href="http$3://$4$5" target="_blank">http$3://$4$5</a>', $foo);
$foo = preg_replace('/(?<!\S)((ftp(7?):\/\/)|(ftp\.))([\w.\/&=?\-~%;]+)\b/i', '<a href="ftp$3://$4$5" target="_blank">ftp$3://$4$5</a>', $foo);
return ($foo);
}

Guna - 29 Aug 2008

Compared to other manuals, I have read this manual explains Simply and in detail. This gives a better understanding of Regular Expression usuage.

I would be more useful, if u give real time examples(extracting emailids, links,etc.,)

foobar - 29 Aug 2008

I had lot of confusions regarding regexp before reading this session. It really helped me. Thanks.

A PHP User - 29 Aug 2008

[A-Z]{1,3}
3 uppercase letters

You must mean 1 to 3 uppercase letters

Dirk - 29 Aug 2008

Nice, but you forget the "|" ( OR ):

if (preg_match("/(php|asp)/", "asp")) {

print "Got it!";

}

matches for "php" OR "asp".

A PHP User - 29 Aug 2008

This site has really helped me to learn some PHP. Thank you!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow