---
title: "Java regex with multiple possible endings"  
description: "Java regex with multiple possible endings"  
author: "Anurag Sharma"  
published: 2015-02-04  
updated: 2015-02-04  
canonical: https://www.mindstick.com/forum/12942/java-regex-with-multiple-possible-endings  
category: ".net"  
tags: ["java", "regex"]  
reading_time: 1 minute  

---

# Java regex with multiple possible endings

I need to write a [regular expression](https://www.mindstick.com/articles/11909/java-regex-or-regular-expression-in-java) that will match filenames that have a txt, htm, or html extension. I know that parens [group](https://yourviews.mindstick.com/view/81323/adani-green-energy-group-bags-world-s-biggest-solar-bid) [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-characters) [together](https://yourviews.mindstick.com/view/80819/live-in-relationship-protecting-the-right-to-live-together) and that brackets allow one [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) from a set to be [matched](https://answers.mindstick.com/qa/95756/what-are-the-benefits-of-google-adsense-matched-content), so I tried [combining](https://yourviews.mindstick.com/view/85223/hybrid-vehicles-combining-fuel-efficiency-and-performance) these approaches, but it is not working. The [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) 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");

}

## Replies

### Reply by Anonymous User

It should be: Example

```
if (!Pattern.matches(".*?\\.(txt|html?)", parts[1]))
{    System.err.println("501 Not Implemented: " + parts[1] + "\n");}
```

- There is no grouping of text inside character class [...]
- matches method assumes anchors so ^ and $ aren't needed.


---

Original Source: https://www.mindstick.com/forum/12942/java-regex-with-multiple-possible-endings

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
