2

I have a string that I am using String.split(regex) to eventually get a string[].

The string format is

January,WEEKDAY,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,AJanuary,WEEKEND,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,B,B,BJanuary,HOLIDAY,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,C,C,CFebruary,WEEKDAY,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,AFebruary,WEEKEND,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,B,B,BFebruary,HOLIDAY,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,AMarch,WEEKDAY,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,C,C,C

The first string after the split should be

January,WEEKDAY,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A

So I'm thinking I need to do a split at either ,A or ,B or ,C that is not followed by a ,

To test the first one, I tried making my regex "(?<!,)A," but that didn't work

Any ideas?

2 Answers 2

1

It seems you're looking for something like the following:

String[] parts = s.split("(?<=,[ABC](?!,))");

Or you can use a word/non-word boundary here as well:

String[] parts = s.split("(?<=\\b[ABC]\\B)");

Ideone Demo

Sign up to request clarification or add additional context in comments.

Comments

0

You can also use (?<=,[ABC])(?=[^,]) to split.

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.