Java:拆分以分号分隔的字符串,但忽略引号中的escape +分号

时间:2016-10-26 07:07:17

标签: java split

我想拆分我的查询,但没有得到完全符合我要求的答案。

我的字符串如下:

  

选择1;选择\\ 2;选择3 \\;从' s复制客户:// mybucket / mydata'凭证' aws_access_key_id = access_key \\; aws_secret_access_key = secret_key \\; master_symmetric_key = master_key'

期望的输出:

  

选择1

     

选择\\ 2

     

选择3 \\

     

从#mybucket / mydata'复制客户凭证' aws_access_key_id = access_key \\; aws_secret_access_key = secret_key \\; master_symmetric_key = master_key'

我找到了关于escaper的解决方案。但它并不符合我的要求。

  

(小于?!\\);

Handling delimiter with escape characters in Java String.split() method

如何在引号中忽略escape +分号?

帮助我。

4 个答案:

答案 0 :(得分:1)

我认为这是一种解决方案:

String line = "select 1;select \\2; select 3\\;copy customer from 's3://mybucket/mydata' credentials 'aws_access_key_id=access_key\\;aws_secret_access_key=secret_key\\;master_symmetric_key=master_key'";
line = line.replace("\\","\\\\");//To avoid missing \
String[] tokens = line.split(";(?=([^']*'[^']*')*[^']*$)");//To split on semmicolons, but not those inside quotes
for(String t : tokens) {
    System.out.println("> "+t);
}

你可以在这里测试一下 http://rextester.com/MLTA75734

答案 1 :(得分:0)

您可以使用外部.jar 比如,commons-lang-2.6.jar

String str = "select 1;select \\2; select 3\\;copy customer from 's3://mybucket/mydata' credentials 'aws_access_key_id=access_key\\;"
            + "aws_secret_access_key=secret_key\\;"
            + "master_symmetric_key=master_key'";
    str = StringEscapeUtils.escapeJavaScript(str); // method from external jar
    String st[] = str.split(";");
    for(int i=0;i<st.length;i++)
    System.out.println(st[i]);

希望它对你有所帮助......

答案 2 :(得分:0)

我尝试了另一种溶剂,这次没有正则表达式。我用尽可能多的奇怪的字符串检查了这个,并且它按照我的预期工作(希望它能像你期望的那样工作),请检查一下。

       String s ="select 1;r;select \\2; select 3\\;copy customer from 's3://mybucket/mydata' credentials 'aws_access_key_id=<access-key-id>\\;aws_secret_access_key=<secret-access-key>\\;master_symmetric_ke‌​y=<master-key>'";
               //"select 1;r;select \\2; select 3\\;copy customer from 'r;s3://mybucket/mydata;r' credentials 'a_key;b_key;c_key\\;r' 'aws_access_key_id=access_key\\;aws_secret_access_key=secret_key\\;master_symmetric_key=master_key'";
       s = s.replace("\\","\\\\");
       List<String> tokens = new ArrayList<String>();               
       int i = 0;    
       int j = 0;
       String backup = s;
       while (i < s.length()){
        char c  = s.charAt(i);      
          if(c==';'){
            String previous = s.substring(0,i);
            int quotesBefore = StringUtils.countMatches(backup.substring(0,j), "'");
            if(i<2 || quotesBefore==0 || (i>1 && (quotesBefore & 1) == 0 || ((quotesBefore & 1) != 0) && !(s.charAt(i-1)=='\\' && s.charAt(i-2)=='\\'))){//Even quotes before OR (odd quotes AND not \\ right before)                 
                tokens.add(previous);
                if(i>0)s=s.substring(i+1);
                i=0;
            }
          }
          i++;j++;
        }
        tokens.add(s);
        for(String t : tokens) {
            System.out.println("> "+t);
        }

基本步骤:

  1. 重写字符串

  2. 每个人检查一下它是否是分号

  3. 如果这是真的,请在此之前获取字符,计算引号并添加 列表中的那些字符只有在奇数或奇数时才会出现 数字,但分号不会使用“\\”
  4. 进行转义

答案 3 :(得分:0)

我使用了以下解决方案,用于带引号('和“)和转义(\)字符的通用刺痛分离器。

public static List<String> split(String str, final char splitChar) {
    List<String> queries = new ArrayList<>();
    int length = str.length();
    int start = 0, current = 0;
    char ch, quoteChar;
    
    while (current < length) {
        ch=str.charAt(current);
        // Handle escape char by skipping next char
        if(ch == '\\') {
            current++;
        }else if(ch == '\'' || ch=='"'){ // Handle quoted values
            quoteChar = ch;
            current++;
            while(current < length) {
                ch = str.charAt(current);
                // Handle escape char by skipping next char
                if (ch == '\\') {
                    current++;
                } else if (ch == quoteChar) {
                    break;
                }
                current++;
            }
        }else if(ch == splitChar) { // Split sting
            queries.add(str.substring(start, current + 1));
            start = current + 1;
        }
        current++;
    }
    // Add last value
    if (start < current) {
        queries.add(str.substring(start));
    }
    return queries;
}

public static void main(String[] args) {

    String str = "select 1;select \\\\2; select 3\\\\;copy customer from 's3://mybucket/mydata' credentials 'aws_access_key_id=access_key\\\\;aws_secret_access_key=secret_key\\\\;master_symmetric_key=master_key'";
    List<String> queries = split(str, ';');
    System.out.println("Size: "+queries.size());
    for (String query : queries) {
        System.out.println(query);
    }
}

得到结果

Size: 4
select 1;
select \\2;
 select 3\\;
copy customer from 's3://mybucket/mydata' credentials 'aws_access_key_id=access_key\\;aws_secret_access_key=secret_key\\;master_symmetric_key=master_key'