我想提取分隔符(在這種情況下是括號內)的字符串發生但不是在引號(單或雙)內的字符串發生。以下是我已經嘗試 - 這個表達式取括號內所有出現的,這也用引號(我不想引號內的)正則表達式來提取分隔符內的字符串
public class RegexMain {
static final String PATTERN = "\\(([^)]+)\\)";
static final Pattern CONTENT = Pattern.compile(PATTERN);
/**
* @param args
*/
public static void main(String[] args) {
String testString = "Rhyme (Jack) and (Jill) went up the hill on \"(Peter's)\" request.";
Matcher match = CONTENT.matcher(testString);
while(match.find()) {
System.out.println(match.group()); // prints Jack, Jill and Peter's
}
}
}
如果我是你,我會首先用空字符串替換引用的部分,然後查找匹配的組。這不是一種選擇嗎? – inhan