David Williams
David Williams

Reputation: 8654

Basic String.match not matching

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

Answers (2)

JDB
JDB

Reputation: 25820

May I recommend a simpler solution?

subDomain.contains("dev")

Upvotes: 3

Stewart
Stewart

Reputation: 18302

String.matches() matches the whole String

I recommend String.startsWith().

Upvotes: 3

Related Questions