簡體   English   中英

Java:String.matches()

[英]Java : 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]+$")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM