場景:java模式使用
Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher(" WhiteSpace");
Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher(" WhiteSpace");
我想在一行的開頭拿到空格。我想要獲得確切數量的空格匹配器。我的字符串是" WhiteSpace"
。
問題是matcher
和matcher2
在這個字符串上工作。
我想要的東西是:
一個模式只能得到1個空格,但這個模式對於2個空格字符串不應該工作 。在下面的情況下,matcher.find()
和matcher2.find()
都是正確的。但matcher.find()
應該是false,matcher2.find()
應該是true。
我想匹配爲" WhiteSpace"
是真實的,虛假的" WhiteSpace"
(兩個空格)
我想matcher2爲真:" WhiteSpace"
。
我想要做的事情是;
我有一個字符串" two whitespaces"
。
下面兩個if語句都是真的。 matcher
應該是錯誤的。 matcher2
應該是正確的。
Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher(" two whitespaces");
Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher(" two whitespaces");
if(matcher.find()==true){
//XXXXXXXXXXX
} else if(matcher2.find()==true){
//YYYYYYYYYYY
}
謝謝Pshemo。它的工作:) – ivbtar