forum

Home / DeveloperSection / Forums / Java regex with multiple possible endings

Java regex with multiple possible endings

Anurag Sharma 1797 04-Feb-2015

I need to write a regular expression that will match filenames that have a txt, htm, or html extension. I know that parens group characters together and that brackets allow one character from a set to be matched, so I tried combining these approaches, but it is not working. The string to be tested is in parts[1]. The .* should match any number of characters, and then \\. would be the dot, and then my attempt at combining brackets and parentheses follows.

if (!Pattern.matches(".*\\.[(txt)(htm)(html)]", parts[1])) {

    System.err.println("501 Not Implemented: " + parts[1] + "\n");

}


Updated on 04-Feb-2015

Can you answer this question?


Answer

1 Answers

Liked By