Regular expressions using character classes : Regular Expressions « String « PHP






Regular expressions using character classes

 
Function call                                         Result
 
preg_match("/[Ff]oo/", "Foo")                         True
 
preg_match("/[^Ff]oo/", "Foo")                        False; 
 
preg_match("/[A-Z][0-9]/", "K9")                      True
 
preg_match("/[A-S]esting/", "Testing")                False; 
 
preg_match("/[A-T]esting/", "Testing")                True; 
 
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; 
 
preg_match("/[^a-z]esting/i", "Testing")              False;
  
  








Related examples in the same category

1.Brackets [] finds a range of characters.
2.Character Classes
3.Complete list of regular expression examples
4.\b and \B, equate to "On a word boundary" and "Not on a word boundary," respectively.
5.^ and $ are line anchors.
6.Line Anchors
7.Match URL
8.Match an IP address
9.Match the smallest number of characters starting with "p" and ending with "t"
10.Matching GUIDs/UUIDs
11.Matching a Valid E-mail Address
12.Matching a Valid IP Address
13.Matching using backreferences
14.Matching with Greedy vs. Nongreedy Expressions
15.Matching with character classes and anchors
16.Matching with |
17.Define a pattern and use parentheses to match individual elements within it
18.Greedy Qualifiers
19.Greedy and non-greedy matching
20.Greedy versus nongreedy matching
21.Grouping captured subpatterns
22.Validating Pascal Case Names
23.Validating U.S. Currency
24.Validating a credit card number
25.Nongreedy Qualifiers
26.POSIX Regular Expressions Character Classes
27.POSIX Regular Expressions Character Classes
28.Ranges
29.Option patterns:
30.Predefined Character Ranges (Character Classes)
31.Pattern matches:
32.Pattern match extenders:
33.Perl-Compatible Regular Expressions (PCRE)
34.Qualifiers restrict the number of times the preceding expression may appear.
35.Quantifiers for Matching a Recurring Character
36.Quantifiers: +, *, ?, {int. range}, and $ follow a character sequence:
37.Special classes for regular expression