Quantcast
Channel: web – Imran Tariq's Blog
Viewing all articles
Browse latest Browse all 33

Special wildcard characters (metacharacters)

$
0
0

Table 1 describes the most important wildcard characters used in regular expressions. Strictly speaking, most of them are metasequences because they are made up of two characters. However, metasequences are commonly referred to as metacharacters.

Table 1. Commonly used metacharacters
Metacharacter     Matches
.     Any single character, except a newline
\d     Any digit character (0-9)
\D     Any non-digit character—in other words, anything except 0-9
\w     Any alphanumeric character or the underscore
\W     Any character, except an alphanumeric character or the underscore
\s     Any white-space character, including space, tab, form feed, or line feed
\S     Any character, except a white-space character
\f     A form feed character
\n     A line feed character
\r     A carriage return character
\t     A tab character

With the exception of the dot (or period), all the wildcard character sequences begin with a backslash. The dot metacharacter matches anything, including a space, punctuation mark, or even itself. The only thing it doesn’t match is a newline. This is a common source of mistakes when composing regular expressions.

Note: ColdFusion is an exception to this rule. In ColdFusion, the dot metacharacter also matches a newline.

The other thing to note about Table 1 is that \d, \w, and \s each have opposites. The uppercase version matches anything not matched by the lowercase version: \d matches a number, \D matches anything except a number. If you get the case of your metacharacter wrong, the regex does the exact opposite of what you intended.


Viewing all articles
Browse latest Browse all 33

Trending Articles