0

I'm having troubles to create a regular expression who checks if a file has the extention .TMP so the pattern should accept any string who's not equal to (a-z 0-9 or event dots).TMP

To be clear: the matcher should only be succesfull when the file doesn't have the TMP extention.

I've allready found that I need to use (?!expression) for the "not"...

(?!.*TMP]) // is wrong ;-)
Stijn Vanpoucke
  • 1,425
  • 3
  • 17
  • 30
  • 2
    Why not match `.TMP` and do stuff when it is *not* matched? – kennytm Aug 11 '10 at 12:15
  • Do you need a regular expression, or do you just need to match files that have a tmp extension? – Jeff Aug 11 '10 at 12:21
  • The code has to be configured with spring, and I wanted to keep all my options open by using regEx. It must be possible to accept files who start with blabla iso looking at all the files who don't end with .TMP in an other implementation when required. – Stijn Vanpoucke Aug 11 '10 at 12:25
  • possible duplicate of [Java Regular Expression, match everything but](http://stackoverflow.com/questions/1061651/java-regular-expression-match-everything-but) – msw Aug 11 '10 at 12:30
  • Makes sense. But in the interest of keeping the Spring context file easy to understand, you might want to consider a method other than regex. .tmp looks nicer than a regex IMO... – Jeff Aug 11 '10 at 12:35

3 Answers3

10
if (!filename.endsWith(".TMP")) {
     /* then we found a match without using regExp */
}
msw
  • 42,753
  • 9
  • 87
  • 112
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
5
"(?<!\\.TMP)\\Z"

Read: something other than ".TMP" followed by the end of the string.

msw
  • 42,753
  • 9
  • 87
  • 112
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • "When all you have is a regexp, every problem looks like line noise" -- me ;) – msw Aug 11 '10 at 12:29
  • 1
    @msw: The question explicitly asked for a regexp. I don't think it's fair to downvote for answering the question that was asked. – sepp2k Aug 11 '10 at 12:31
  • Agreed. It wasn't to "punish" the poster but to move the answer down the stack (which has happened naturally thus downvote removed). Indeed, the question did ask for a regexp, but sometimes the question is wrong. I don't expect you to necessarily agree with this stance, but I hope you can understand it. – msw Aug 11 '10 at 12:54
2

It is not a answer of your question but I think you should look at Apache Common IO which have a bunch of simple methods which can do everything your commonly do. Including finding extensions. Then you simply make a if statement instead of a reg. exp.

http://commons.apache.org/io/

and see the specific java doc for getExtension: getExtension JavaDoc

Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99