Regex – Match Start or End of String (Line Anchors)

1. Line Anchors

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.

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.
RegexStringMatches
^aabcMatches a
c$abcMatches c
^[a-zA-Z]+$abcMatches abc
^[abc]$ abcMatches a or b or c
[^abc]abcDoes not match. A matching string begins with any character but a,b,c.
^[mts][aeiou]motherMatches. 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$ kongMatches.
^g.+g$gangMatches. Word would start and end with g. Any number of letters in between.

See Also: Java regex to allow only alphanumeric characters

2. Regex to Match Start of Line

"^<insertPatternHere>"
  • The caret ^ matches the position before the first character in the string.
  • Applying ^h to howtodoinjava matches h.
  • Applying ^t to howtodoinjava does not match anything because it expects the string to start with t.
  • 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.
DescriptionMatching 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 matches a.
  • Applying v$ to howtodoinjava does not match anything because it expects the string to end with a.
  • 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.
DescriptionMatching 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 !!

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial