java 分割字符串
在Java中,如果我有一个具有这种格式的字符串:
( string1 , string2 ) ( string2 ) ( string4 , string5 , string6 ) [s2]
如何拆分字符串以获得这样的字符串数组?
string1 , string2
string2
string4 , string5 , string6
in Java, if I have a string with this format:
( string1 , string2 ) ( string2 ) ( string4 , string5 , string6 ) [s2]
How can I split the string to get an array of string as this?
string1 , string2
string2
string4 , string5 , string6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
我很OK2024-12-21 08:47:25
您可以使用匹配:
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("\\((.*?)\\)");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group(1));
}
匹配 () 之间的任何内容并将其存储到反向引用 1 中。
说明:
"\\(" + // Match the character “(” literally
"(" + // Match the regular expression below and capture its match into backreference number 1
"." + // Match any single character that is not a line break character
"*?" + // Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
")" +
"\\)" // Match the character “)” literally
无畏2024-12-21 08:47:25
您可能想在 /\(.+?\)/
上使用 split - 在 java 中类似这样:
Pattern p = Pattern.compile("\\(.+?\\)");
Matcher m = p.matcher(myString);
ArrayList<String> ar = new ArrayList<String>();
while (m.find()) {
ar.add(m.group());
}
String[] result = new String[ar.size()];
result = ar.toArray(result);
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
试试这个:
Try this: