如何使用正则表达式匹配括号内的文本?
问题描述:
我有以下模式:
(COMPANY) -277.9887 (ASP,) -277.9887 (INC.)
我希望最终输出为:
COMPANY ASP,INC。
COMPANY ASP, INC.
目前我有以下代码并且它不断返回原始模式(我假设因为该组都在第一个'('和最后一个'之间) )'
Currently I have the following code and it keeps returning the original pattern ( I assume because the group all falls between the first '(' and last ')'
Pattern p = Pattern.compile("((.*))",Pattern.DOTALL);
Matcher matcher = p.matcher(eName);
while(matcher.find())
{
System.out.println("found match:"+matcher.group(1));
}
我很难得到我需要的结果并感谢任何帮助。我得到每个小组后,我不担心连接结果,只需要得到每个小组。
I am struggling to get the results I need and appreciate any help. I am not worried about concatenating the results after I get each group, just need to get each group.
答
Pattern p = Pattern.compile("\\((.*?)\\)",Pattern.DOTALL);