我要解决的问题如下: 我有这个字符串作为输入:
Hello World:[[DATA.[field]]]其他内容
我想要捕捉的组是:
数据。[字段]
问题是我想捕获“[[”和“]]”内的任何内容。
我应该在 Java 中使用什么正则表达式? 谢谢
最佳答案
尝试以下正则表达式:
(?<=\[\[).*?(?=\]\](\s|$))
示例:
String text = "Hello World: [[DATA.[field]]] something else";
Pattern regex = Pattern.compile("(?<=\\[\\[).*?(?=\\]\\](\\s|$))", Pattern.MULTILINE);
Matcher regexMatcher = regex.matcher(text);
if (regexMatcher.find()) {
System.out.println(regexMatcher.group());
}
从示例数据字符串中,它将打印
DATA.[field]
关于java - 如果有内括号,如何将一对括号内的内容与正则表达式匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36312557/