Split and replace Java string

I am trying to read a text file, split the contents as described below, and add split comments to the Java list.

Error in terms of splitting.

Existing line:

a1(X1, UniqueVar1), a2(X2, UniqueVar1), a3(UniqueVar1, UniqueVar2)

Expected - break them and add to your Java list:

a1(X1, UniqueVar1)
a2(X2, UniqueVar1)
a3(UniqueVar1, UniqueVar2)

the code:

subSplit = obj.split("\\), ");
for (String subObj: subSplit)
{
    System.out.println(subObj.trim());
}

Result:

a1(X1, UniqueVar1
a2(X2, UniqueVar1
...

Please suggest how to fix this.

+4
source share
1 answer

Use a positive lookbehind in your regex:

String[] subSplit = obj.split("(?<=\\)), ");

This expression matches the ,one preceded ), but since the lookbehind part does (?<=\\))not capture (zero width), it is not discarded as part of the delimiter delimiter.

, , javadoc Pattern.

+9

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


All Articles