如何使用正则表达式匹配括号内的文本? [英] How do I match text within parentheses using regex?

查看:485
本文介绍了如何使用正则表达式匹配括号内的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模式:

(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);

这篇关于如何使用正则表达式匹配括号内的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆