且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java-如何测试字符串是否同时包含字母和数字

更新时间:2023-02-19 13:10:22

我怀疑下面的正则表达式会因环顾而变慢,但是无论如何它都可以工作:

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.