我有一个String包含2或3个公司名称,每个名称都括在括号中.每个公司名称也可以包含括号中的单词.我需要使用正则表达式将它们分开,但没有找到.
我的inputStr:
(Motor (Sport) (racing) Ltd.) (Motorsport racing (Ltd.)) (Motorsport racing Ltd.)
or
(Motor (Sport) (racing) Ltd.) (Motorsport racing (Ltd.))
Run Code Online (Sandbox Code Playgroud)
预期的结果是:
str1 = Motor (Sport) (racing) Ltd.
str2 = Motorsport racing (Ltd.)
str3 = Motorsport racing Ltd.
Run Code Online (Sandbox Code Playgroud)
我的代码:
String str1, str2, str3;
Pattern p = Pattern.compile("\\((.*?)\\)");
Matcher m = p.matcher(inputStr);
int index = 0;
while(m.find()) {
String text = m.group(1);
text = text != null && StringUtils.countMatches(text, "(") != StringUtils.countMatches(text, ")") ? text + ")" : …Run Code Online (Sandbox Code Playgroud)