About metacharacters
A metacharacter is one or more characters with a special, non-literal meaning in the context of a regular expression. For example, the circumflex character (^) is a metacharacter that means "search at the beginning". If you want to find the circumflex character, rather than the metacharacter pattern, you must protect (or "escape") it with a backslash: \^.
The table following lists examples of some common regular expression metacharacters.
Metacharacter Description |
Example |
|
---|---|---|
\ |
Protect the next character (interpret it literally and not as a metacharacter). |
\! finds an exclamation mark (!) \. finds a period (.), rather than a character |
^ |
Find at the beginning of a string. |
^H finds Home but not home or PHP |
. |
Find any character (letter, number, symbol, whitespace), except newline. |
(.*) finds any paragraph |
( ) |
Find a pattern group. |
(word) finds "In this word" ^(Word) finds "Words in this line" Welcome ((back)|(home)) finds "Welcome back" and "Welcome home" |
[ ] |
Find a pattern range. |
[a-z] finds any lower-case alphabetic character |
* |
Find the pattern zero or more times. |
<(.*)> finds all HTML tags, with their content |
+ |
Find the pattern one or more times. |
(<.l>)+ finds <UL><OL> |
? |
Find the pattern zero or one time. |
log(.?)in finds login and log in |
(?i) |
Find the next characters with a case-insensitive search. |
(?i)word finds word, Word, woRd, WORD |