简体   繁体   中英

Java : String.matches()

I want to determine whether a string consists of only two words, with one space character between them. The first word should only include alphanumeric characters. The second should include only numbers. An example is: asd 15

I would like to use String.matches . How can I do this or which regex should I use?

Thanks in advance.

You look for something like:

String regex = "^[A-Za-z]+ [0-9]+$";

Explanation:

^         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]+$")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM