java - 用于分隔括号中的字符串的正则表达式
问题描述
我有一个String
包含 2 或 3 个公司名称,每个都用括号括起来。每个公司名称还可以包含括号中的单词。我需要使用正则表达式将它们分开,但没有找到方法。
我的inputStr
:
(Motor (Sport) (racing) Ltd.) (Motorsport racing (Ltd.)) (Motorsport racing Ltd.)
or
(Motor (Sport) (racing) Ltd.) (Motorsport racing (Ltd.))
预期结果是:
str1 = Motor (Sport) (racing) Ltd.
str2 = Motorsport racing (Ltd.)
str3 = Motorsport racing Ltd.
我的代码:
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 + ")" : text;
if (index == 0) {
str1= text;
} else if (index == 1) {
str2 = text;
} else if (index == 2) {
str3 = text;
}
index++;
}
这适用于str2
但str3
不适用于str1
.
当前结果:
str1 = Motor (Sport)
str2 = Motorsport racing (Ltd.)
str3 = Motorsport racing Ltd.
解决方案
你可以不用正则表达式来解决这个问题;请参阅有关如何找到最外层括号的问题。
这是一个例子:
import java.util.Stack;
public class Main {
public static void main(String[] args) {
String input = "(Motor (Sport) (racing) Ltd.) (Motorsport racing (Ltd.)) (Motorsport racing Ltd.)";
for (int index = 0; index < input.length(); ) {
if (input.charAt(index) == '(') {
int close = findClose(input, index); // find the close parentheses
System.out.println(input.substring(index + 1, close));
index = close + 1; // skip content and nested parentheses
} else {
index++;
}
}
}
private static int findClose(String input, int start) {
Stack<Integer> stack = new Stack<>();
for (int index = start; index < input.length(); index++) {
if (input.charAt(index) == '(') {
stack.push(index);
} else if (input.charAt(index) == ')') {
stack.pop();
if (stack.isEmpty()) {
return index;
}
}
}
// unreachable if your parentheses is balanced
return 0;
}
}
输出:
Motor (Sport) (racing) Ltd.
Motorsport racing (Ltd.)
Motorsport racing Ltd.
推荐阅读
- c# - ASP.NET Core:在使用自定义 DbContext 的同时也使用 Identity 时出错
- node.js - Firebase 函数引用错误
- java - Jena(在 Ubuntu 上)为 ModelFactory 提供 NoClassDefFoundError
- gcloud - 使用 gcloud cli 创建服务帐号密钥时权限被拒绝
- python - 使用 Python 连接到 Office 365 Sharepoint 时出现 AADSTS70002、AADSTS50053 错误
- xml - XML - XSLT - 使用辅助文档和
- datetime - 将谷歌表单日期和时间合并到日期时间
- python - SQLAlchemy index names unique
- angular - 使用 HttpClient 和 RXJS 将数据发布到 Web API withCredentials: true
- python - 在 Python 中使用字典创建变量