Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : String.matches()

Tags:

java

string

regex

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.

like image 932
Predoryx Avatar asked Aug 04 '26 01:08

Predoryx


1 Answers

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)
like image 98
MByD Avatar answered Aug 06 '26 15:08

MByD