[英]Regular expression to match a string between @@ in Java
我正在使用Java Pattern Compile將子字符串與特定模式匹配,並將其添加到列表中。
我希望正則表達式匹配以下情況
當前代碼是:-
List<String> matches = new ArrayList<String>();
Matcher m = Pattern.compile("@@([^@@]*)@@")
.matcher(content);
System.out.println(m.find());
while (m.find()) {
matches.add(m.group());
}
我的內容是'<script> var bookSrc = "@@book:URL@@",var books_count: @@Count@@, var active = true</script>'
上面的代碼僅匹配@@之間的字符串,實際結果為[@@ count @@],我希望結果為[@@ book:URL @@,@@ count @@]
任何幫助將不勝感激,我應該如何修改我的模式以適應所有上述情況。
您可以在執行循環之前調用Matcher.find
。 因此,將跳過第一個比賽。 如果刪除
System.out.println(m.find());
它按預期工作。 您仍然可以測試循環后是否存在匹配項:
System.out.println(!matches.isEmpty());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.