且构网

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

如何分析java中的字符串以确保它有字母和数字?

更新时间:2023-02-19 13:01:59

您的问题有点模棱两可。

Your question is a little ambiguous.

如果您只想知道字符串是否包含字母或数字(但可能仅 字母或 数字)没有别的,那么正则表达式:

If you just want to know whether the string contains letters or numbers (but perhaps only letters or only numbers) and nothing else, then the regex :

pass.matches("^[a-zA-Z0-9]+$");

将返回true。

如果你想检查它是否包含两个字母和数字,没有别的,那么它更复杂,但是类似于:

If you want to check whether it contains both letters and numbers, and nothing else, then it is more complex, but something like:

pass.matches("^[a-zA-Z0-9]*(([a-zA-Z][0-9])|([0-9][a-zA-Z]))[a-zA-Z0-9]*$");

所有***的