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 Top answer 1 of 3
20
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
2 of 3
1
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
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
15:23
Validating input strings in Java with Regular Expressions - YouTube
08:18
REGULAR EXPRESSIONS IN JAVA | Java Regex Tutorial | Java Training ...
08:21
The Regex in Java | Complete Java Course for Beginners #18 - YouTube
26:29
EVERYTHING you need to know about REGULAR EXPRESSIONS in JAVA in ...
REGEX TO VALIDATE USER INPUT IN JAVA (Part 2)| RegEx ...
06:24
Java Password Validation using Regular Expression - Pattern and ...
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.
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 ...
Baeldung
baeldung.com › home › java › java string › regular expression for password validation in java
Regular Expression for Password Validation in Java | Baeldung
June 20, 2024 - Learn how to use a regex for Java-based password validation processes.
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:
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 ...
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: