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

Answer from Valentin Genevrais on Stack Overflow
🌐
Apache Commons
commons.apache.org › proper › commons-validator › jacoco › org.apache.commons.validator.routines › RegexValidator.java.html
RegexValidator.java - Apache Commons
For example to create a validator which does <em>case in-sensitive</em> validation * for a set of regular expressions: * </p> * * <pre> * <code> * String[] regexs = new String[] {...}; * RegexValidator validator = new RegexValidator(regexs, false); * </code> * </pre> * * <ul> * <li>Validate {@code true} or {@code false}:</li> * <li> * <ul> * <li>{@code boolean valid = validator.isValid(value);}</li> * </ul> * </li> * <li>Validate returning an aggregated String of the matched groups:</li> * <li> * <ul> * <li>{@code String result = validator.validate(value);}</li> * </ul> * </li> * <li>Validate
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-validate-a-username-using-regular-expressions-in-java
How to validate a Username using Regular Expressions in Java - GeeksforGeeks
July 12, 2025 - Return true if the string matches with the given regex, else return false. Below is the implementation of the above approach: Java · // Java program to validate a username // using Regular Expression or ReGex import java.util.regex.*; class ...
🌐
Okta Developer
developer.okta.com › blog › 2022 › 04 › 19 › java-regex
A Quick Guide to Regular Expressions in Java | Okta Developer
April 19, 2022 - To use a community pattern, you must first select a pattern, click on its URL, or double-click the list to load the full pattern. You can also use the right arrow icon to load the expression or text. In the screenshot below, we have picked an existing community pattern for password validation. We have run some tests to ensure that it does proper password validation as per the regex.
🌐
Baeldung
baeldung.com › home › java › java string › email validation in java
Email Validation in Java | Baeldung
July 16, 2026 - The RFC 5322, which is an updated version of RFC 822, provides a regular expression for email validation. ... As we can see, it’s a very simple regex that allows all the characters in the email.
🌐
Medium
medium.com › @kaustubh.saha › regular-expressions-in-java-b806152aaa93
Regular Expressions in Java. A regular expression is essentially a… | by Kaustubh Saha | Medium
November 29, 2025 - In Java, regex is primarily used for three things: Validation: Checking if an input string matches a required format (e.g., passwords, zip codes).
🌐
Regex101
regex101.com
regex101: build, test, and debug regex
Online regex tester and debugger with real-time match highlighting, detailed explanations, substitutions, unit tests, benchmarking, and code generation. Supports PCRE2, JavaScript, Python, Go, Java, .NET, Rust, POSIX ERE, and POSIX BRE.
🌐
Apache Commons
commons.apache.org › proper › commons-validator › apidocs › org › apache › commons › validator › routines › RegexValidator.html
RegexValidator (Apache Commons Validator 1.10.2-SNAPSHOT API)
By default, validation is case sensitive but constructors are provided to allow case in-sensitive validation. For example to create a validator which does case in-sensitive validation for a set of regular expressions: String[] regexs = new String[] {...}; RegexValidator validator = new ...
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › matches
Java String matches() - Check Regex Match | Vultr Docs
December 17, 2024 - The matches() method in Java is an essential tool for checking if a given string complies with the pattern defined by a regular expression (regex). This method returns a boolean indicating whether the string matches the regex pattern entirely, ...
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java regex tutorial with regular expression examples
Java Regex Tutorial With Regular Expression Examples
April 1, 2025 - Answer: The package java.util.regex provides a Pattern class that is used to compile a regex into a pattern which is the standard representation for regex. This pattern is then used to validate strings by matching it with the pattern.
🌐
Medium
medium.com › @python-javascript-php-html-css › guide-to-java-email-validation-regex-f9bccc290395
Guide to Java Email Validation Regex | by Denis Bélanger
August 24, 2024 - This discussion will explore a ... to enhance its effectiveness. ... The Java script uses the Pattern.compile() method to create a regex pattern, which is a crucial part of the email validation process....
🌐
GeeksforGeeks
geeksforgeeks.org › java › regular-expressions-in-java
Regular Expressions in Java - GeeksforGeeks
May 22, 2026 - Regex can split text, validate input, and extract data efficiently in Java.
🌐
Medium
medium.com › stackera › java-regex-part-3-matching-text-for-validation-cont-cfd917e5411e
Java RegEx: Part 3— Matching text for validation (Cont.) | by Sera Ng. | Tech Training Space | Medium
October 18, 2020 - Java RegEx: Part 3— Matching text for validation (Cont.) In this session, we are going to use a regular expression to perform validation on some common inputs: phone numbers and email …
🌐
Codementor
codementor.io › community › java regular expression: part 2 - matching text for validation 1
Java Regular Expression: part 2 - Matching text for validation 1 | Codementor
July 9, 2019 - From the outputs: 1: invalid because the pattern requires at least 2 digits 1001: invalid because the pattern allows maximum of 3 digits 33: valid because it mathes the defined pattern · In this case, we will ask users to input a string pattern starting with certain characters followed by certain digits. Let’s pick ISBN as an example. Suppose we want users to input a book ISBN with the following pattern: ... Followed by 5 digits Some examples: ISBN-12345, ISBN-98765 We can use the following code to achieve the task: import java.util.Scanner; public class Demo { public static void main(Strin
🌐
Stack Overflow
stackoverflow.com › questions › 61724734 › java-regex-validator
Java Regex validator - Stack Overflow
But there are a few things that can be improved with your regex. First, the way you have it defined, it would accept as valid input:
🌐
Mkyong
mkyong.com › home › regular expressions › java regex validate username examples
Java regex validate username examples | mkyong.com
November 5, 2020 - This article shows how to use regex to validate a username in Java. Username requirements Username consists of alphanumeric characters (a-zA-Z0-9), lowercase, or uppercase. Username allowed of the dot (.),
🌐
Coderanch
coderanch.com › t › 380861 › java › Validation-Regex
Validation using Regex (Java in General forum at Coderanch)
As for regular expressions, as good a place to start as any is in the JavaDocs for the java.util.regex package. Getting a regular expression evaluation tool for your prefered IDE speeds things up no end too. Here is one for Eclipse. ... Boost this thread! ... Validation ...
🌐
Baeldung
baeldung.com › home › java › core java › validate phone numbers with java regex
Validate Phone Numbers With Java Regex | Baeldung
January 8, 2024 - If we are unable to create a common regular expression that can validate all the possible cases that we want to cover, we can define different expressions for each of the cases and then use them all together by concatenating them with a pipe symbol (|).
🌐
Stack Abuse
stackabuse.com › guide-to-regular-expressions-in-java
Guide to Regular Expressions in Java
October 24, 2023 - Knowing this, to validate an email address using RegEx in Java, we'll compile the expression and use the matches() method to check whether it's valid: