Java正则表达式验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44584622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:36:30  来源:igfitidea点击:

Java Regular expression validation

javaregexstring

提问by user3518223

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

我想验证一个字符串,它只允许字母数字值和 java 中只允许一个点字符和下划线字符。

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

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

我需要验证从请求中检索的 fileName 并且应该满足上述条件

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

我试过了"^[a-zA-Z0-9_'.']*$",但这允许多个点字符

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

1 . 文件名仅包含字母数字值。2 . 它只允许一个点字符 (.) ,例如: fileRead.pdf 、 fileWrite.txt 等 3 。它只允许下划线字符。应拒绝所有其他符号

Can any one help me on this ?

谁可以帮我这个事 ?

采纳答案by Valentin Genevrais

You should use String.matches()method :

您应该使用String.matches()方法:

System.out.println("My_File_Name.txt".matches("\w+\.\w+"));

You can also use java.util.regexpackage.

您还可以使用java.util.regex包。

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

有关REGEX 和 JAVA 的更多信息,请查看此页面:https: //docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

回答by ahmetcetin

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.

如果它与您的请求不匹配,您可以在正则表达式检查之前将其丢弃。如果没有问题,您现在可以将字符串进行字母数字值检查。

回答by Tim Biegeleisen

You could use two negative lookaheads here:

您可以在这里使用两个负面的前瞻:

^((?!.*\..*\.)(?!.*_.*_)[A-Za-z0-9_.])*$

Each lookahead asserts that either a dot or an underscore does notoccur two times, implying that it can occur at most once.

每个先行断言,无论是点或下划线并不会出现两次,这意味着它可以最多出现一次。

It wasn't completely clear whether you requireone dot and/or underscore. I assumed not, but my regex could be easily modified to this requirement.

不完全清楚您是否需要一个点和/或下划线。我假设不是,但我的正则表达式可以很容易地修改为这个要求。

Demo

演示