Regular Expressions
Matching the Beginning of the Line: The Caret (^)
When the caret character ^ is used as the first character in a regular expression, it matches the beginning of the line. So the regular expression
^Ashish : matches the characters Ashish only if they occur at the beginning of the line.
Matching the End of the Line: The Dollar Sign ($)
Just as the ^ is used to match the beginning of the line, so is the dollar sign $ used to match the end of the line. So the regular expression
contents$ : matches the characters contents only if they are the last characters on the line. What do you think would be matched by the regular expression .$?
Would this match a period character that ends a line? No. This matches any single character at the end of the line (including a period) recalling that the period matches any character. So how do you match a period? In general, if you want to match any of the characters that have a special meaning in forming regular expressions, you must precede the character by a backslash (\) to remove that special meaning. So the regular expression
\.$
Comments
Post a Comment