Need a code using regular expression in java --> String must be Captial letters(A-Z) and contain digits (0-9) [closed]

空扰寡人 提交于 2019-12-14 03:36:53

问题


I need a regular expression for:

String must be Captial letters(A-Z) and contain digits (0-9)


回答1:


The following should work:

[A-Z0-9]+



回答2:


^[A-Z]+[0-9]*$

Should match one or more capital letters followed by zero or more digits.

It would have been nice though if you:

-Told us exactly what you needed

-Gave us samples of what you expected

-What exactly have you tried...

Edit: If you want a regex that will match any string that has digits, capital letters AND white spaces without any particular order, then this should work:

^[A-Z0-9 ]*$

This will match 0 or more repetitions of capital letters, numbers and white spaces. If you want the string to contain at least a digit, whitespace or capital letter, then, this should do the trick:

^[A-Z0-9 ]+$

As opposed to the * operator which matches 0 or more repetitions, the + will match 1 or more. The ^ tells the regular expression to start matching from the very beginning of the string while the $ tells the regex to end the pattern matching at the very end of the string.



来源:https://stackoverflow.com/questions/5314777/need-a-code-using-regular-expression-in-java-string-must-be-captial-letters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!