Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex : matching a char except when preceded by another char

Tags:

java

regex

I'm trying to use String.Split() to split a query, in that case a HiveQL query.

The case I have is I want to split along ; except when that ; is preceded by a \. My problem :

String.Split(";") 

is not enough.

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

(i.e not a \ followed by a ;) applied on

select table; count table; 

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

Is there any solution ?

like image 843
C4stor Avatar asked Jun 07 '13 13:06

C4stor


People also ask

What does \\ mean in Java regex?

Backslashes in Java. The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.

What does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.

What is the difference between matches () and find () in Java regex?

Difference between matches() and find() in Java RegexThe matches() method returns true If the regular expression matches the whole text. If not, the matches() method returns false. Whereas find() search for the occurrence of the regular expression passes to Pattern.

Which regular expression do you use to match one or more of the preceding characters?

The character + in a regular expression means "match the preceding character one or more times". For example A+ matches one or more of character A. The plus character, used in a regular expression, is called a Kleene plus .


1 Answers

You need a negative lookbehind for that:

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

Here is a demo on ideone.

like image 125
Sergey Kalinichenko Avatar answered Oct 13 '22 19:10

Sergey Kalinichenko