''在java中標識轉義字符,所以如果你想自己使用它,你需要轉義它。 (\ t是選項卡,\ n是換行等) 爲了逃避它,你需要添加1「\」,而不是2
下面是一些對你的Java代碼將這樣的伎倆:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.junit.Test;
public class RegexTest {
@Test
public void testPatternWithWhiteSpace(){
Pattern pattern = Pattern.compile("Powered\\s*[bB]y.*MyBB");
Matcher matcher = pattern.matcher("Powered By MyBB");
Assert.assertTrue(matcher.matches());
matcher = pattern.matcher("Powered_By MyBB");
Assert.assertFalse(matcher.matches());
}
}
您不必將空白'標記'(\ s)放在字符集([])中。
你是對的,\ s是任何空格。
正則表達式好友需要輸入,因爲正則表達式實際上是寫入的,eclipse /源代碼中的字符串表達式需要被轉義。 如果你想從一個屬性文件中讀取模式,你不需要轉義它。
正如我所說的,正則表達式的好友也承認 它是這樣的,如果我用\ S是不 在正則表達式好友公認因此 測試失敗
我假設你的意思月蝕在那裏失敗? 這些線路只需添加到測試,也許這可以解釋它多一點:
matcher = pattern.matcher("Powered\t\n\r By MyBB");
Assert.assertTrue(matcher.matches());
matcher = pattern.matcher("Powered\t\n\r ,By MyBB");
Assert.assertFalse(matcher.matches());
「Eclipse」是一個工具。你是用Java,JavaScript,Python編寫代碼嗎?或者它在Eclipse的查找工具中? – PhiLho 2008-12-30 14:40:01