【问题标题】:Regex : Split on comma , but exclude commas within parentheses and quotes(Both single & Double)正则表达式:在逗号上拆分,但在括号和引号内排除逗号(单引号和双引号)
【发布时间】:2015-02-18 15:19:40
【问题描述】:

我有一个字符串

5,(5,5),C'A,B','A,B',',B','A,',"A,B",C"A,B" 

我想用逗号分割它,但需要在括号和引号中排除逗号(单引号和双引号)。

像这样

5(5,5)C'A,B''A,B'',B''A,'"A,B"C"A,B"

使用java正则表达式如何实现这个??

【问题讨论】:

    标签: java regex


    【解决方案1】:

    你可以使用这个正则表达式:

    String input = "5,(5,5),C'A,B','A,B',',B','A,',\"A,B\",C\"A,B\"";
    String[] toks = input.split( 
                    ",(?=(([^']*'){2})*[^']*$)(?=(([^\"]*\"){2})*[^\"]*$)(?![^()]*\\))" );
    for (String tok: toks)
        System.out.printf("<%s>%n", tok);
    

    输出:

    <5>
    <(5,5)>
    <C'A,B'>
    <'A,B'>
    <',B'>
    <'A,'>
    <"A,B">
    <C"A,B">
    

    说明:

    ,                         # Match literal comma
    (?=(([^']*'){2})*[^']*$)  # Lookahead to ensure comma is followed by even number of '
    (?=(([^"]*"){2})*[^"]*$)  # Lookahead to ensure comma is followed by even number of "
    (?![^()]*\\))             # Negative lookahead to ensure ) is not followed by matching
                              # all non [()] characters in between
    

    【讨论】:

    • 如果我给嵌套括号它在括号内的逗号上吐痰......5(5,5(5,R5))。
    • 这不适用于 嵌套括号 字符串。没有正则表达式可以帮助你。
    【解决方案2】:
    ,(?![^(]*\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)
    

    试试这个。

    demo

    对于java

    ,(?![^(]*\\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)
    

    【讨论】:

    • Eclipse 显示 --invalid 转义序列(有效的是 \b \t \n \f \r \" \' \\ )
    • @anupammaiti ,(?![^(]*\\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)
    【解决方案3】:

    考虑匹配而不是splitting 字符串。

    String s  = "5,(5,5),C'A,B','A,B',',B','A,',\"A,B\",C\"A,B\"";
    Pattern p = Pattern.compile("(?:[^,]*(['\"])[^'\"]*\\1|\\([^)]*\\))|[^,]+");
    Matcher m = p.matcher(s);
    while (m.find()) {
      System.out.println(m.group());
    }
    

    输出

    5
    (5,5)
    C'A,B'
    'A,B'
    ',B'
    'A,'
    "A,B" 
    C"A,B"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-29
      • 2019-07-05
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      相关资源
      最近更新 更多