PHP Regular Expressions Character Classes
Description
Regular expressions allow you to form character classes of words using brackets [].
Syntax
Put character classes into a [].
Simple Group
[Ff] will match "F" or "f".
The following expression matches "a" , "b", "c", "1", "2", or "3":
<?PHP
echo preg_match( "/[abc123]/", "b" );
?>
The code above generates the following result.
Ranges of characters
Specify ranges of characters using the hyphen (-) symbol.
- [A?Z] will match all uppercase letters.
- [A?Za?z] will match all letters, whether uppercase or lowercase.
- [a?z0?9] will match lowercase letters and numbers only.
The following expression matches "a" , "b", "c", "1", "2", or "3":
<?PHP
echo preg_match( "/[a-c1-3]/", "b" ); // Displays "1"
?>
So you can match any letter or digit using:
<?PHP
echo preg_match( "/[a-zA-Z0-9]/", "H" ); // Displays "1"
?>
Not in the range
The caret symbol ^ means "not,"
- [^A?Z] will match everything that is not an uppercase letter,
- [^A?Za?z0?9] will match symbols but not uppercase letters, not lowercase letters, and not numbers.
To negate the sense of a character class. that is, to match a character that is not one of the characters in the set, place a caret (^ ) symbol at the start of the list:
<?PHP
echo preg_match( "/[abc]/", "e" ) . "\n"; // Displays "0"
echo preg_match( "/[^abc]/", "e" ) . "\n"; // Displays "1"
?>
The code above generates the following result.
Shorthand character classes
Various shorthand character classes comprising a backslash followed by one of several letters
Character Class | Meaning |
---|---|
\d | A digit |
\D | Any character that isn't a digit |
\w | A word character (letter, digit, or underscore) |
\W | Any character that isn't a word character |
\s | A whitespace character (space, tab, line feed, carriage return, or form feed) |
\S | Any character that isn't a whitespace character |
So to match a digit character anywhere in the target string you could use either of the following two expressions:
/[0-9]/
/\d/
Incidentally, you can also use a shorthand character class within a longhand class. The following expression matches the letter " e" or " p " , or any digit, in the target string:
/[ep\d]/
Here are some examples:
<?PHP//from www .j a v a2s .c om
echo preg_match( "/\d[A-Z]/", "3D" ); // Displays "1"
echo preg_match( "/\d[A-Z]/", "CD" ); // Displays "0"
echo preg_match( "/\S\S\S/", "6 & c" ); // Displays "1"
echo preg_match( "/\S\S\S/", "6 c" ); // Displays "0"
?>
To match any character at all, use a dot ( . ):
<?PHP
echo preg_match( "/He.../", "Hello" ); // Displays "1"
?>
Example
Regular expressions using character classes
Function call | Result |
---|---|
preg_match("/[Ff]oo/", "Foo") | True |
preg_match("/[^Ff]oo/", "foo") | False; the regexp says "Anything that is not F or f, followed by "oo". |
preg_match("/[A-Z][0-9]/", "A9") | True |
preg_match("/[A-S]esting/", "Testing") | False; the acceptable range for the first character ends at S |
preg_match("/[A-T]esting/", "Testing") | True; the range is inclusive |
preg_match("/[a-z]esting[0-9][0-9]/","TestingAA") | False |
preg_match("/[a-z]esting[0-9][0-9]/","testing99") | True |
preg_match("/[a-z]esting[0-9][0-9]/","Testing99") | False; case sensitivity! |
preg_match("/[a-z]esting[0-9][0-9]/i","Testing99") | True; case problems fixed with /i |
preg_match("/[^a-z]esting/", "Testing") | True; first character can be anything that is not a, b, c, d, e,etc. (lowercase) |
preg_match("/[^a-z]esting/i","Testing") | False; the range excludes lowercase characters only. the "i" at the end makes it insensitive, which turns [^a-z] into [^a-zA-Z] |