If you want to find the occurrence of a pattern in more precise position, for example at the beginning or the end of line, you can use boundary matcher. Boundary matcher are special sequences in a regular expression when you want to match a particular boundary.
Here are the list:
Matcher | Matches |
---|---|
^ |
The beginning of line |
$ |
The end of line |
b |
A word boundary |
B |
A non word boundary |
A |
The beginning of the input |
G |
The end of previous match |
Z |
The end of the input but for the final terminator, if any |
z |
The end of the input |
Some examples:
^Java
will find the word Java at the beginning of any line.Java$
will find the word Java at the end of any line.\bJ..a\b
will find the word beginning with'J'
and ending with'a'
.
package org.kodejava.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BoundaryMatcherDemo {
public static void main(String[] args) {
// Define regex to find the word "dog" at the end of the line.
String regex = "dog$";
// Compiles the pattern and obtains the matcher object.
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(
"The quick brown fox jumps over the lazy dog");
// Find every match and print it
while (matcher.find()) {
System.out.format("Text \"%s\" found at %d to %d.%n",
matcher.group(), matcher.start(), matcher.end());
}
}
}
This program output the following result:
Text "dog" found at 40 to 43.
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024