- Special characters in Regular Expressions – Part 1
- Regular Expressions – Starting from basics
With this post, we continue to explore the Regular expressions. The first post of the Learning Regular Expression series introduced Regular Expressions. The first post covers the regular expression delimiters and the “i” pattern modifier. In the language of regular expression, there is a special meaning of certain characters.
In the first post, the expressions matching was equivalent to matching strings. These special characters turn the strings into patterns and help in defining stronger search patterns. In this post, we will learn how these special characters convert a string into a pattern.
Continuing with the example pattern
we will introduce three special characters in this pattern. The three special characters are caret (^), asterisk (*) and dot (.). Now, we will add these characters to the example pattern.
When this pattern is passed to the
preg_match function of PHP, then it will match all the strings starting with ref. It will match strings like refulz, refulz PHP and even reference, which starts with ref. So, what does each special character stands for.
Caret Symbol in Regular Expressions
The caret (^) sign means the start of the string. So, the pattern characters are to be matched at the start of the string being searched/matched.
So, if we remove the caret sign, then the resulting pattern will also match strings like preference.
Dot Symbol in Regular Expression
The dot (.) symbol indicates one occurrence of any character. So, if the pattern becomes
then, it will match refu from refulz string. This is because it is looking for a subject string that begins with ref and matches any single character after that.
Asterisk Symbol in Regular Expression
The asterisk (*) symbol means zero or more occurrences of the preceding character. So, in the pattern
the asterisk will search for zero or more occurrences of any character (indicated by the dot symbol).
Dollar ($) Symbol in Regular Expression
Introducing another special character, we will see what the dollar ($) symbol does to a regular expression pattern. The dollar sign complements the caret sign; it marks the end of the string.
So, the above pattern will only match a string that ends with ref. It can be a long string like “For your ref”. The input string should end with the pattern string.
In the next post of the series, we will continue to familiarise with other special characters.