6

I want to validate a string which allows only alpha numeric values and only one dot character and only underscore character in java .

String fileName = (String) request.getParameter("read");

I need to validate the fileName retrieving from the request and should satisfy the above criteria

I tried in "^[a-zA-Z0-9_'.']*$" , but this allows more than one dot character

I need to validate my string in the given scenarios ,

1 . Filename contains only alpha numeric values . 2 . It allows only one dot character (.) , example : fileRead.pdf , fileWrite.txt etc 3 . it allows only underscore characters . All the other symbols should be declined

Can any one help me on this ?

4
  • 2
    can you share an input output example please? Commented Jun 16, 2017 at 8:39
  • regex101.com this online service lets you try out regex and explains the single characters and what they do in a regex really nice. It's very good for self study :) Commented Jun 16, 2017 at 8:41
  • you should point whether the filename contains file extension. your pattern allows last dot and first dot. Commented Jun 16, 2017 at 8:54
  • You do realize that both Windows and Unix/Linux/OSX allow filenames with more than one dot character? Is there a specific purpose for your special restriction? Commented Jun 16, 2017 at 9:19

3 Answers 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

Sign up to request clarification or add additional context in comments.

4 Comments

Don't use matcher.find() but matcher.matches(). The regex part of your answer accepts input like ßαd.txt (or worse: .........a.txt). If you use matches(), that won't be the case anymore.
you must escape backslash double.
Thanks you Olivier
Thanks you mattn :)
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

Comments

0

You can first check the special characters which have the number limits. Here is the code:

int occurance = StringUtils.countOccurrencesOf("123123..32131.3", ".");

or

int count = StringUtils.countMatches("123123..32131.3", ".");

If it does not match your request you can discard it before regex check. If there is no problem you can now put your String to alphanumeric value check.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.