1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

The Three Most Basic Regular Expressions

The three simplest regular expressions you will use in PHP


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".

  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Advanced PHP
  5. Regular Expressions - Basic Regular Expressions - Most Simple Regular Expressions

©2014 About.com. All rights reserved.