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 ?
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.
(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.
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.
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 .
You need a negative lookbehind for that:
String.Split("(?<![\\\\]);");
Here is a demo on ideone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With