【发布时间】:2012-04-22 12:49:44
【问题描述】:
我想确定一个字符串是否仅由两个单词组成,它们之间有一个空格字符。第一个单词应仅包含字母数字字符。第二个应该只包括数字。一个例子是:asd 15
我想使用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]+$")
【讨论】:
^[0-9A-Za-z]+...。