java 分割字符串

发布于 12-14 08:47 字数 236 浏览 2 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时光的容颜2024-12-21 08:47:25

试试这个:

    String test = "( string1 , string2 ) ( string2 ) ( string4 , string5 , string6 ) [s2]";

    String[] splits = test.split("\\(\\s*|\\)[^\\(]*\\(?\\s*");

    for (String split : splits) {
        System.out.println(split);
    }

Try this:

    String test = "( string1 , string2 ) ( string2 ) ( string4 , string5 , string6 ) [s2]";

    String[] splits = test.split("\\(\\s*|\\)[^\\(]*\\(?\\s*");

    for (String split : splits) {
        System.out.println(split);
    }
我很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

You can use a match :

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("\\((.*?)\\)");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group(1));
} 

Matches anything between () and stores it into backreference 1.

Explanation :

 "\\(" +      // 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);

You may want to use split on /\(.+?\)/ - something like this in 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文