简体   繁体   English

Java regex:匹配一个字符,除非前面有另一个字符

[英]Java regex : matching a char except when preceded by another char

I'm trying to use String.Split() to split a query, in that case a HiveQL query.我正在尝试使用 String.Split() 来拆分查询,在这种情况下是 HiveQL 查询。

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("[^\\\\];") 

(ie 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 ;会给组"select tabl"" count tabl" ,所以我失去了之前的字符; . .

Is there any solution ?有什么解决办法吗?

You need a negative lookbehind for that:你需要一个负面的回顾

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

Here is a demo on ideone .这是ideone 上演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM