Reputation: 8654
Java's String.match
is not behaving the way I think it should and I'm not sure why. Here is what I'm doing, I'm trying to parse the subdomain of a host and do something conditionally if the subdomain matches "dev".
String serverName = httpServletRequest.getServerName();
List<String> partsList = new ArrayList<>(Arrays.asList(serverName.split("\\.")));
if(partsList.size() == 3) {
String subDomain = partsList.get(0);
System.out.println(subDomain + " => " + subDomain.matches("dev"));
The thing is, this prints the following to the console:
dev01 => false
What is going on here? It clearly does match. Am I using the regex string incorrectly?
Upvotes: 0
Views: 69
Reputation: 18302
String.matches()
matches the whole String
I recommend String.startsWith()
.
Upvotes: 3