文字列が2つの単語だけで構成されており、その間にスペース文字が1つあるかどうかを確認したいと思います。最初の単語には英数字のみを含める必要があります。2番目には数字のみを含める必要があります。例は次のとおりです。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]+$")