问题描述
我需要一个能同时满足这两个条件的正则表达式.
I need a regex which will satisfy both conditions.
只有当字符串同时包含A-Z和0-9时,它才应该为真.
It should give me true only when a String contains both A-Z and 0-9.
这是我尝试过的:
如果PNo [0] .matches("^ [A-Z0-9] + $")
它不起作用.
推荐答案
我怀疑下面的正则表达式会因环顾而变慢,但是无论如何它都可以工作:
I suspect that the regex below is slowed down by the look-around, but it should work regardless:
.matches("^(?=.*[A-Z])(?=.*[0-9])[A-Z0-9]+$")
正则表达式断言字符串中某处有一个大写字母字符(?=.* [AZ])
,并断言有一个数字(?=.* [0-9])
在字符串中的某个位置,然后检查是否所有内容都是字母字符还是数字.
The regex asserts that there is an uppercase alphabetical character (?=.*[A-Z])
somewhere in the string, and asserts that there is a digit (?=.*[0-9])
somewhere in the string, and then it checks whether everything is either alphabetical character or digit.
这篇关于java-如何测试字符串是否同时包含字母和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!