Question: I have a string of comma-delimited characters that I'm splitting. Some of these characters, though, could be commas. For example:

`test = "a,b,c,d,,,e,f,g"`

I know that `(?<!,),` is the regular expression for "any commas not preceded by a comma", and `,(?!,)` is the regular expression for "any comma followed by a comma". Could someone point me in the right direction and show me how to combine these two. The desired output is: 

```
a  
b  
c  
d  
,  
e  
f  
g  

```

The program is in Java, so if someone knows a function specific to Java, that works, too.

Answer: 
Split by `,` only if there is no `,` before or after it .

```
    String str = "a,b,c,d,,,e,f,g";
    String regex = "(?<!,),|,(?!,)";

    for(String s : str.split(regex)) {            
        System.out.println(s);
    }

```

Output:

> 
> 
> ```
> a
> b
> c
> d
> ,
> e
> f
> g
> 
> ```
> 
>
the following will find a character followed by a comma and then remove the final comma by only taking the first character:

```
        String test = "a,b,c,d,,,e,f,g";
        Pattern p=Pattern.compile(".,|.$");
        Matcher m=p.matcher(test);
        while(m.find() ){                    
            System.out.println(m.group().charAt(0));
        }

```