1. Home
  2. Computing & Technology
  3. PHP / MySQL

The Three Most Basic Regular Expressions
The three simplest regular expressions you will use in PHP

By Angela Bradley, About.com

The three most basic searches using regular expressions are searching for the strings start, the strings end, or for characters it contains. We can do these using the following symbols:

^ indicates the string starts with the specified characters, so "^About" would match any string starting with "About". This would match "About.com is nice" or "About the pirates" but would not match "out and about".

$ indicates that a string finishes with the specified characters, so "about$" would match any string that ended with "about". This what match "What is it about" or "Out and about" but would not match "About.com is nice"

"About" looks for the characters specified within the quotes, anywhere within the string. This means not only would it match "About.com is nice" and "out and about" it would match "Something About Mary"

These can be combined and used together. Let's say that you only wanted to match the word About, and not any sentances it may be contained in. You might try combining start and end searches like this:

"^About$" This would match only strings that both start and end with About, which applies to a the word "About".

More PHP / MySQL Quick Tips
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Advanced PHP
  5. Regular Expressions - Basic Regular Expressions - Most Simple Regular Expressions

©2009 About.com, a part of The New York Times Company.

All rights reserved.