Java regex: char matching unless another char precedes

I am trying to use String.Split () to split a query, in this case a HiveQL query.

I want to divide the case that I have into ; , except when preceded by \ . My problem:

 String.Split(";") 

not enough.

 String.Split("[^\\\\];") 

(i.e. not \ followed by ; ) applied to

 select table; count table; 

will give the groups "select tabl" , " count tabl" , so I lose the character before ; .

Is there any solution?

+6
source share
1 answer

For this you need a negative lookbehind :

 String.Split("(?<![\\\\]);"); 

Here is the daemon on ideone .

+10
source

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


All Articles