Regular line splitting with ignored patterns

I have a source line that I want to break dataout:

String source = "data|junk,data|junk|junk,data,data|junk";
String[] result = source.split(",");

The above gives data|junk, data|junk|junk, data, data|junk. To get the data, I did the following:

for (int i = 0; i < result.length; i++) {
    result[i] = result[i].split("\\|")[0];
}

Which gives what I wanted data, data, data, data. I want to see if this can be done in the same split with the correct regular expression:

String[] result = source.split("\\|.*?,");

The above gives data, data, data,data|junkin which the last two data are not split. Could you help with the correct regular expression to get the result I wanted?

Example line: "Ann | xcjiajeaw, Bob | aijife | vdsjisdjfe, Clara, David | rijfidjf"

Expected Result: "Anne, Bob, Clara, David"

+4
source share
3 answers

In line

Ann | xcjiajeaw, | aijife | vdsjisdjfe, , | rijfidjf

\\|.*?, - |anynoncommastring, |rijfidjf, . , (,|$) ,, \\|.*?(,|$)

, , \\|.*?(,|$), (\\|.*?(,|$)|,).

(\\|.*?(,|$)|,) ,

String source = "Ann|xcjiajeaw,Bob|aijife|vdsjisdjfe,Clara,David|rijfidjf";
String[] result = source.split("(\\|.*?(,|$)|,)");
for (int i = 0; i < result.length; i++) {
    System.out.println(result[i]);
}

:

Ann
Bob
Clara
David
+2

" ", , :

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexTest {
    public static void main(String[] args) {
        String input = "Ann|xcjiajeaw,Bob|aijife|vdsjisdjfe,Clara,David|rijfidjf";
        Pattern p = Pattern.compile("(\\w+)(\\|\\w+)*,?");
        Matcher m = p.matcher(input);
        while (m.find()) {
            System.out.println(m.group(1));
        }
    }
}

(, ) . ( , ) . ( ) . , .

Ann

Clara

"", m.group(2) . , ?: :

Pattern.compile("(\\w+)(?:\\|\\w+)*,?");
+3

I came up with the following solution:

String source = "one|junk,two|junk|junk,three,four|junk|junk";
String[] result = source.split("([|](?:(.*?,(?=[^,]+[|,]|$))|.*$))|,");
System.out.println(Arrays.toString(result));

[one, two, three, four]
+1
source

Source: https://habr.com/ru/post/1695186/


All Articles