PHP Regular Expressions Using Braces – String Function

Regexp                                                       Result
preg_match("/[A-Z]{3}/", "FuZ")                              False; the regexp will match precisely three uppercase letters
preg_match("/[A-Z]{3}/i", "FuZ")                             True; same as above, but case-insensitive this time
preg_match("/[0-9]{3}-[0-9]{4}/", "555-1234")                True; precisely three numbers, a dash, then precisely four. This will match local U.S. telephone numbers, for example
preg_match("/[a-z]+[0-9]?[a-z]{1}/", "aaa1")                 True; must end with one lowercase letter
preg_match("/[A-Z]{1,}99/", "99")                            False; must start with at least one uppercase letter
preg_match("/[A-Z]{1,5}99/", "FINGERS99")                    True; "S99", "RS99", "ERS99", "GERS99", and "NGERS99" all fit the criteria
preg_match("/[A-Z]{1,5}[0-9]{2}/i", "adams42")               True

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.