【发布时间】:2019-01-30 04:17:09
【问题描述】:
我正在尝试提取方形或圆形内的字符串。字符串可能只有方括号或圆括号
我正在使用下面的正则表达式。
Pattern p = Pattern.compile("\\[(.*?)\\]|\\((.*?)\\)");
输出字符串也包括括号。下面是代码。
String example = "Example_(xxxxx)_AND_(yyyyy)_2019-01-28";
Pattern p = Pattern.compile("\\[(.*?)\\]|\\((.*?)\\)");
Matcher m = p.matcher(example);
while(m.find()) {
System.out.println(m.group(1));
}
上述模式给出的输出为
(xxxxx)
(yyyyy)
预期输出是
xxxxx
yyyyy
【问题讨论】:
-
第 1 组应打印两次
null。在这种情况下,第 2 组将具有您需要的输出。