Regular expressions, commonly referred to as regex, are powerful tools for pattern matching and manipulation of text. The regex patterns enable developers to perform intricate string searches, substitutions, and validations. Among the plethora of regex features, line anchors are the key components for precisely identifying the beginning or end of a string.
In regex, line anchors possess zero width and are not intended for matching characters within a line. Instead, they pinpoint specific positions – either the start or the end of the line.
In this article, we’ll delve into the line anchors and understand how they work in regex.
// List of sentences
String[] sentences = {
"The sun is shining", //0
"Apples are delicious", //1
"The moon is bright", //2
"Birds are singing" //3
};
// Start line anchor (^)
String beginWithPattern = "^The"; //Matches lines 0 and 2
// End line anchor ($)
String endWithPattern = "ing$"; //Matches lines 0 and 3
1. Line Anchors
Line anchors are regex constructs used to assert the position of a string relative to the start or end of a line.
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. It ensures that the specified pattern occurs right at the start of a line, without any preceding characters.
- Dollar ($): matches the position right after the last character in the string. It ensures that the specified pattern occurs right before the end of a line, with no characters following it.
To understand line anchors better, let’s explore some simple examples:
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 the 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 the 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 !!
Comments