ComlyW
ComlyW

Reputation: 77

Java regex substring not followed by comma character

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?

Upvotes: 2

Views: 257

Answers (2)

hwnd
hwnd

Reputation: 70732

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

Upvotes: 1

karthik manchala
karthik manchala

Reputation: 13640

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

Upvotes: 0

Related Questions