In regex, the anchors have zero width. They are not used for matching characters. Rather they match a position i.e. before, after, or between characters.
1. Line Anchors
To match the start or the end of a line, we use the following anchors:
- Caret (^) matches the position before the first character in the string.
- Dollar ($) matches the position right after the last character in the string.
Regex | String | Matches |
---|---|---|
^a | abc | Matches a |
c$ | abc | Matches c |
^[a-zA-Z]+$ | abc | Matches abc |
^[abc]$ | abc | Matches a or b or c |
[^abc] | abc | Does not match. A matching string begins with any character but a,b,c. |
^[mts][aeiou] | mother | Matches. Searches for words that start with m, t or s. Then immediately followed by a vowel. |
[^n]g$ | king ng | Does not match. The string should end with g, but not ng. |
[^k]g$ | kong | Matches. |
^g.+g$ | gang | Matches. Word would start and end with g. Any number of letters in between. |
2. Regex to Match Start of Line
"^<insertPatternHere>"
- The caret
^
matches the position before the first character in the string. - Applying
^h
to howtodoinjava matchesh
. - Applying
^t
to howtodoinjava does not match anything because it expects the string to start witht
. - If we have a multi-line string, by default caret symbol matches the position before the very first character in the whole string. To match the position before the first character of any line, we must enable the multi-line mode in the regular expression.
In this case, caret changes from matching at only the start the entire string to the start of any line within the string.
Description | Matching Pattern |
---|---|
The line starts with a number | “^\\d” or “^[0-9]” |
The line starts with a character | “^[a-z]” or “^[A-Z]” |
The line starts with a character (case-insensitive) | “^[a-zA-Z]” |
The line starts with a word | “^word” |
The line starts with a special character | “^[!@#\\$%\\^\\&*\\)\\(+=._-]” |
Pattern.compile("^[0-9]").matcher("1stKnight").find();
Pattern.compile("^[a-zA-Z]").matcher("FirstKnight").find();
Pattern.compile("^First").matcher("FirstKnight").find();
Pattern.compile("^[!@#\\$%\\^\\&*\\)\\(+=._-]").matcher("*1stKnight").find();
Program output.
true
true
true
true
3. Regex to Match End of Line
"<insertPatternHere>$"
- The dollar
$
matches the position after the last character in the string. - Applying
a$
to howtodoinjava matchesa
. - Applying
v$
to howtodoinjava does not match anything because it expects the string to end withv
. - If we have a multi-line string, by default dollar symbol matches the position after the very last character in the whole string.
To match the position after the last character of any line, we must enable the multi-line mode in the regular expression. In this case, dollar changes from matching at only the last the entire string to the last of any line within the string.
Description | Matching Pattern |
---|---|
The line ends with a number | “\\d$” or “[0-9]$” |
The line ends with a character | “[a-z]$” or “[A-Z]$” |
The line ends with a character (case-insensitive) | [a-zA-Z]$ |
The line ends with a word | “word$” |
The line ends with a special character | “[!@#\\$%\\^\\&*\\)\\(+=._-]$” |
Pattern.compile("[0-9]$").matcher("FirstKnight123").find();
Pattern.compile("[a-zA-Z]$").matcher("FirstKnight").find();
Pattern.compile("Knight$").matcher("FirstKnight").find();
Pattern.compile("[!@#\\$%\\^\\&*\\)\\(+=._-]$")
.matcher("FirstKnight&").find();
Program output.
true
true
true
true
Drop me your questions related to programs for regex starts with and ends with java.
Happy Learning !!