考慮源文本之間的所有文字獲取{和}(原文包含 n)的
{
a1
a2
a3
}
{
b1
b2
b3
}
我需要得到的2項:
a1
a2
a3
和
b1
b2
b3
使用正則表達式。
我tryed這樣一個解決方案:
java regular expression to extract content within square brackets
但沒有更迭,因爲原來的文本包含 '\ n' 字符。
考慮源文本之間的所有文字獲取{和}(原文包含 n)的
{
a1
a2
a3
}
{
b1
b2
b3
}
我需要得到的2項:
a1
a2
a3
和
b1
b2
b3
使用正則表達式。
我tryed這樣一個解決方案:
java regular expression to extract content within square brackets
但沒有更迭,因爲原來的文本包含 '\ n' 字符。
也許你可以使用這個表達式:
\\{\\n([^\\}]+)\\n\\}
測試here。
如果你想整個代碼,其中s
就是包含您的源文本:
Pattern p = Pattern.compile("\\{\\n([^\\}]+)\\n\\}");
Matcher m = p.matcher(s);
while (m.find()){
System.out.println(m.group());
}
好!我應該使用Java中的.split()函數來獲得正確的結果嗎? –
@Seraphim我在我的答案中添加了代碼部分。我對Java的語言不太熟悉(正則表達式在許多不同的語言中都是它自己的語言),但我認爲這是可行的。 – Jerry
是的!好!它正在工作! –
使用'[^}]''而不是*'?即'\\ {([^}])\\}' – Bakuriu