If you ever use regular expressions, this website is a must have tool: online regular expression tester.
Java Regex pattern that matches in any online tester but doesn't in Eclipse - Stack Overflow
regex - Java Regular expression validation - Stack Overflow
Online Regex tester and debugger for multiple languages - One of my favourite sites!
You should use String.matches() method :
System.out.println("My_File_Name.txt".matches("\\w+\\.\\w+"));
You can also use java.util.regex package.
java.util.regex.Pattern pattern =
java.util.regex.Pattern.compile("\\w+\\.\\w+");
java.util.regex.Matcher matcher = pattern.matcher("My_File_Name.txt");
System.out.println(matcher.matches());
For more information about REGEX and JAVA, look at this page : https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
You could use two negative lookaheads here:
^((?!.*\..*\.)(?!.*_.*_)[A-Za-z0-9_.])*$
Each lookahead asserts that either a dot or an underscore does not occur two times, implying that it can occur at most once.
It wasn't completely clear whether you require one dot and/or underscore. I assumed not, but my regex could be easily modified to this requirement.
Demo
After testing on both [regex101 and RegExr] my test cases are detected properly
That seems unlikely, since your pattern is indeed faulty, not only in Java's Regex dialect but also in the ones tested by those sites. The only plausible explanation I see is that you were not actually testing the cases you think you were. For example, your test inputs may have had trailing spaces or newlines.
Which brings me to the problem with your pattern. As you already observe,
Now the intersting part is is when I remove [^.] it will detect,
That's because that sub-expression matches a character (different from .). Your overall pattern therefore indeed does not match "gruell-Core.exe" because there is no character after the .exe. Try matching "gruell-Core.exee" instead.
If you want your matches to end with .exe, then anchor your pattern instead: gruell.*\.exe$
Alright thanks to the site provided by John Bollinger https://www.regexplanet.com/advanced/java/index.html I was able to find out 2 things that were wrong here.
First off I had to use:
pattern.matcher(file.name).matches()
Instead of what I had:
pattern.matcher(file.name).find()
And second off I had to remove [^.] from the end of the String.
From:
"gruell.*\\.exe[^.]"
To:
"gruell.*\\.exe"