我的文字像這樣的字符串:Java正則表達式查找字符串把它添加到陣列,然後替換原來的
This is a[WAIT] test.
我想要做的是搜索字符串與啓動子[和結尾] 每一個我覺得我想它在原始字符串以^
這添加到一個ArrayList和替換子是我的正則表達式:
String regex_script = "/^\\[\\]$/"; //Match a string which starts with the character [ ending in the character ]
這裏是我的到目前爲止:
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile(regex_script); // Create a pattern to match
Matcher m = p.matcher(line); // Create a matcher with an input string
boolean result = m.find();
while(result) {
m.appendReplacement(sb, "^");
result = m.find();
}
m.appendTail(sb); // Add the last segment of input to the new String
我該怎麼做到這一點?謝謝
工作得很好!感謝 - 這個問題並不熟悉某些轉義序列如何影響模式。 – GideonKain
@ouotuo嗨,你可以看看這個問題嗎? http://stackoverflow.com/questions/34938232/android-regex-passing-of-text-output-from-method-to-method –