Java Matcher lookingAt() Method
Example
Check if two texts start with "Hello":
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("Hello");
Matcher matcher1 = pattern.matcher("Hello World");
Matcher matcher2 = pattern.matcher("Say Hello World");
System.out.println(matcher1.lookingAt());
System.out.println(matcher2.lookingAt());
}
}
Definition and Usage
The lookingAt() method checks if the beginning of the text matches the pattern.
Only the start of the text must match, not the whole text. This is different from matches(), where the whole text must match, and from find(), which looks for the pattern anywhere in the text.
Syntax
public boolean lookingAt()
Parameter Values
None.
Technical Details
| Returns: | true if the beginning of the text matches the pattern, otherwise false. |
|---|
Related Pages
❮ RegEx Methods