The two special symbols: '^' and '$' indicate the start and the end of a string respectively, like so:
Without either of the above special character you are allowing the pattern to occur anywhere inside the string.
The symbols '*', '+', and '?' denote the number of times a character or a sequence of characters may occur. What they mean is: zero or more, one or more, and zero or one. Here are some examples:
You can also use bounds, which come inside braces and indicate ranges in the number of occurences:
Note, that you must always specify the first number of a range (i.e, {0,2}, not {,2}). Also, as you may have noticed, the symbols '*', '+', and '?' have the same effect as using the bounds {0,}, {1,}, and {0,1}, respectively.
Now, to quantify a sequence of characters, put them inside parentheses:
There's also the '|' symbol, which works as an OR operator:
A period ('.') stands for any single character:
Bracket expressions specify which characters are allowed in a single position of a string:
You can also list the characters that do NOT want -- just use a '^' as the first symbol in a bracketed expression (i.e., %[^a-zA-Z]% matches a string with a character that is not a letter between two percent signs).
Do not forget that bracket expressions are an exception to that rule--inside them, all special characters, including the backslash ('\'), lose their special powers (i.e., [*\+?{}.] matches exactly any of the characters inside the brackets). To include a literal ']' in the list, make it the first character (following a possible '^'). To include a literal '-', make it the first or last character, or the second endpoint of a range.
Previous
|
Chapter Contents |
Next
Server & client versions compatibility |