Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

PHP Tutorial

PHP HOME PHP Intro PHP Install PHP Syntax PHP Comments PHP Variables PHP Echo / Print PHP Data Types PHP Strings PHP Numbers PHP Casting PHP Math PHP Constants PHP Magic Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP Loops PHP Functions PHP Arrays PHP Superglobals PHP RegEx PHP RegEx Functions

PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

PHP Advanced

PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions

PHP OOP

PHP What is OOP PHP Classes/Objects PHP Constructor PHP Destructor PHP Access Modifiers PHP Inheritance PHP Constants PHP Abstract Classes PHP Interfaces PHP Traits PHP Static Methods PHP Static Properties PHP Namespaces PHP Iterables

MySQL Database

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll

PHP Examples

PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Server PHP Syllabus PHP Study Plan PHP Certificate

PHP Reference

PHP Overview PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Exception PHP Filesystem PHP Filter PHP FTP PHP JSON PHP Keywords PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP Network PHP Output Control PHP RegEx PHP SimpleXML PHP Stream PHP String PHP Variable Handling PHP XML Parser PHP Zip PHP Timezones

PHP Regular Expressions


PHP Regular Expressions

A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for.

A regular expression can be a single character, or a more complicated pattern.

Regular expressions can be used to perform all types of text search and text replace operations.


Regular Expression Syntax

In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers.

Syntax

"/pattern/modifiers"
  • delimiters - charcters that enclose the pattern (e.g. /)
  • pattern - the character sequence to search for
  • modifiers - how the search is performed (e.g. i indicates a case-insensitive search)

Look at the following regular expression:

$exp = "/w3schools/i";

Here, / is the delimiter, w3schools is the pattern to search for, and i is a modifier that makes the search case-insensitive.

The delimiter can be any character that is not a letter, number, backslash or space. The most common delimiter is the forward slash (/), but when your pattern contains forward slashes it is convenient to choose other delimiters such as # or ~.



Regular Expression Modifiers

The modifiers specify how the search is performed.

Modifier Description Try it
i Performs a case-insensitive search Try it »
m Performs a multiline search (patterns that search for a match at the beginning or end of a string will now match the beginning or end of each line) Try it »
u Enables correct matching of UTF-8 encoded patterns

Regular Expression Patterns

Brackets are used to find a range of characters:

Expression Description Try it
[abc] Find one or many of the characters inside the brackets Try it »
[^abc] Find any character NOT between the brackets Try it »
[a-z] Find any character alphabetically between two letters Try it »
[A-z] Find any character alphabetically between a specified upper-case letter and a specified lower-case letter Try it »
[A-Z] Find any character alphabetically between two upper-case letters. Try it »
[123] Find one or many of the digits inside the brackets Try it »
[0-5] Find any digits between the two numbers Try it »
[0-9] Find any digits Try it »

Metacharacters

Metacharacters are characters with a special meaning:

Metacharacter Description Try it
| Find a match for any one of the patterns separated by | as in: cat|dog|fish Try it »
. Find any character Try it »
^ Finds a match as the beginning of a string as in: ^Hello Try it »
$ Finds a match at the end of the string as in: World$ Try it »
\d Find any digits Try it »
\D Find any non-digits Try it »
\s Find any whitespace character Try it »
\S Find any non-whitespace character Try it »
\w Find any alphabetical letter (a to Z) and digit (0 to 9) Try it »
\W Find any non-alphabetical and non-digit character Try it »
\b Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b Try it »
\uxxxx Find the Unicode character specified by the hexadecimal number xxxx Try it »

Quantifiers

Quantifiers define quantities:

Quantifier Description Try it
n+ Matches any string that contains at least one n Try it »
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n{3} Matches any string that contains a sequence of 3 n's Try it »
n{2, 5} Matches any string that contains a sequence of at least 2, but not more that 5 n's Try it »
n{3,} Matches any string that contains a sequence of at least 3 n's Try it »

Note: If your expression needs to search for one of the special characters you can use a backslash ( \ ) to escape them. For example, to search for one or more question marks you can use the following expression: $pattern = '/\?+/';


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.