The example below demonstrate the Matcher.lookingAt()
method to check if a string starts with a pattern represented by the Pattern
class.
package org.kodejava.regex;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherLookingAtExample {
public static void main(String[] args) {
// Get the available countries
Set<String> countries = new TreeSet<>();
Locale[] locales = Locale.getAvailableLocales();
for (Locale locale : locales) {
countries.add(locale.getDisplayCountry());
}
// Create a Pattern instance. Look for a country that start with
// "I" with an arbitrary second letter and have either "a" or "e"
// letter in the next sequence.
Pattern pattern = Pattern.compile("^I.[ae]");
System.out.println("Country name which have the pattern of " +
pattern.pattern() + ": ");
// Find country name which prefix matches the matcher's pattern
for (String country : countries) {
// Create matcher object
Matcher matcher = pattern.matcher(country);
// Check if the matcher's prefix match with the matcher's
// pattern
if (matcher.lookingAt()) {
System.out.println("Found: " + country);
}
}
}
}
The following country names is printed as the result of the program above:
Country name which have the pattern of ^I.[ae]:
Found: Iceland
Found: Iran
Found: Iraq
Found: Ireland
Found: Italy
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