Reputation: 51
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.
Upvotes: 3
Views: 11132
Reputation: 136
Try this out: String pattern = new String("^[0-9]*[A-Za-z]+ [0-9]+$")
Upvotes: 0
Reputation: 137412
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)
Upvotes: 7
Reputation: 4704
You can give this regex a try
"[A-Za-z0-9]+\\s[0-9]+"
Upvotes: 4