php

Pattern Matching



Regular expressions (regex or regexp) are powerful tools for pattern matching and string manipulation. Here are some commonly used regular expression patterns along with explanations:

 

Brackets are used to search for specific range of characters.

Expression

Description

[rgb]

Matches any single character within the brackets

[^rgb]

Matches any single character not within brackets

[0-9]

Matches any number

 

Metacharacters

Metacharacters are characters that have a special meaning and are used to define the structure of the pattern.

 

Some metacharacters are as follows: 

Character

Function

.

Matches a single character

\s

It matches a space, newline, tab

\S

Non-whitespace character

\d

Matches any digit from 0 to 9

\D

Matches a non-digit character

\w

Matches for a word character as a-z, A-Z, 0-9