[英]Java : String.matches()
我想确定一个字符串是否只包含两个单词,它们之间有一个空格字符。 第一个单词应该只包含字母数字字符。 第二个应该只包括数字。 一个例子是: asd 15
我想使用String.matches
。 我该怎么做或者我应该使用哪个正则表达式?
提前致谢。
你寻找像这样的东西:
String regex = "^[A-Za-z]+ [0-9]+$";
说明:
^ the beginning of the string (nothing before the next token)
[A-Za-z]+ at least one, but maybe more alphabetic chars (case insensitive)
a single space (hard to see :) )
[0-9]+ at least one, but maybe more digits
$ end of the string (nothing after the digits)
你可以试试这个正则表达式
"[A-Za-z0-9]+\\s[0-9]+"
试试这个: String pattern = new String("^[0-9]*[A-Za-z]+ [0-9]+$")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.