0

I have tried to split a string using split method, but I'm facing some problem in using split method.

String str="1-DRYBEANS,2-PLAINRICE,3-COLDCEREAL,4-HOTCEREAL,51-ASSORTEDETHNIC,GOURMET&SPECIALTY";

List<String> zoneArray = new ArrayList<>(Arrays.asList(zoneDescTemp.split(",")));

Actual output :

zoneArray = {"1-DRYBEANS","2-PLAINRICE","3-COLDCEREAL","4-HOTCEREAL","51-ASSORTEDETHNIC","GOURMET&SPECIALTY"}

Expected output :

zoneArray = {"1-DRYBEANS","2-PLAINRICE","3-COLDCEREAL","4-HOTCEREAL","51-ASSORTEDETHNIC,GOURMET&SPECIALTY"}

Any help would be appreciated.

2
  • You'll need to use a regular expression that contains a look-ahead, a regex that looks for a comma (not in the look-ahead) followed by a number (in the look-ahead) Commented Oct 12, 2019 at 14:05
  • Relevant tutorial page Commented Oct 12, 2019 at 14:06

2 Answers 2

3

Use split(",(?=[0-9])")

You are not just splitting by comma, but splitting by comma only if it is followed by a digit from 0-9. This is also known as positive lookahead (?=).

Take a look at this code snippet for example:

public static void main(String[] args) {
        String str="1-DRYBEANS,2-PLAINRICE,3-COLDCEREAL,4-HOTCEREAL,51-ASSORTEDETHNIC,GOURMET&SPECIALTY";

        String[] array1= str.split(",(?=[0-9])");
        for (String temp: array1){
            System.out.println(temp);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, exactly. 1+
3

Use a look-ahead within your regex, one that uses comma (not in the look-ahead), followed by a number (in the look-head). \\d+ will suffice for number. The regex can look like:

String regex = ",(?=\\d+)";

For example:

public class Foo {
    public static void main(String[] args) {
        String str = "1-DRYBEANS,2-PLAINRICE,3-COLDCEREAL,4-HOTCEREAL,51-ASSORTEDETHNIC,GOURMET&SPECIALTY";
        String regex = ",(?=\\d+)";
        String[] tokens = str.split(regex);
        for (String item : tokens) {
            System.out.println(item);
        }
    }
}

what this does is split on a comma that is followed by numbers, but does not remove from the output, the numbers since they are part of the look-ahead.

For more on look-ahead, look-behind and look-around, please check out this relevant tutorial page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.