【问题标题】:Explain working Regex expression解释工作正则表达式
【发布时间】:2011-12-22 15:14:48
【问题描述】:

如果包含双引号,则发现此代码会拆分 CSV 字段 但我不太了解正则表达式的模式匹配

如果有人可以逐步解释此表达式如何评估模式,将不胜感激

"([^\"]*)"|(?<=,|^)([^,]*)(?:,|$)

谢谢

==== 旧帖

这对我来说效果很好 - 它可以匹配“两个引号以及它们之间的任何内容”,或者“行首或逗号与行尾或逗号之间的某个内容”。遍历匹配项可以获得所有字段,即使它们是空的。例如,

快速,“棕色,狐狸跳”,over,“the”,“lazy dog”分解成

快速的“棕色,狐狸跳”过“那只”“懒狗”

import java.util.ArrayList; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class CSVParser { 

    /* 
     * This Pattern will match on either quoted text or text between commas, including 
     * whitespace, and accounting for beginning and end of line. 
     */ 
    private final Pattern csvPattern = Pattern.compile("\"([^\"]*)\"|(?<=,|^)([^,]*)(?:,|$)");   
    private ArrayList<String> allMatches = null;         
    private Matcher matcher = null; 
    private String match = null; 
    private int size; 

    public CSVParser() {                 
        allMatches = new ArrayList<String>(); 
        matcher = null; 
        match = null; 
    } 

    public String[] parse(String csvLine) { 
        matcher = csvPattern.matcher(csvLine); 
        allMatches.clear(); 
        String match; 
        while (matcher.find()) { 
                match = matcher.group(1); 
                if (match!=null) { 
                        allMatches.add(match); 
                } 
                else { 
                        allMatches.add(matcher.group(2)); 
                } 
        } 

        size = allMatches.size();                
        if (size > 0) { 
                return allMatches.toArray(new String[size]); 
        } 
        else { 
                return new String[0]; 
        }                        
    }    

    public static void main(String[] args) {             
        String lineinput = "the quick,\"brown, fox jumps\",over,\"the\",,\"lazy dog\""; 

        CSVParser myCSV = new CSVParser(); 
        System.out.println("Testing CSVParser with: \n " + lineinput); 
        for (String s : myCSV.parse(lineinput)) { 
                System.out.println(s); 
        } 
    } 

} 

【问题讨论】:

  • 如果需要,链接到另一个答案比在没有任何参考的情况下复制整个内容更有用。

标签: java regex csv


【解决方案1】:

我尝试在regular-expressions.info 上为您提供提示和所需词汇以找到很好的解释

"([^\"]*)"|(?<=,|^)([^,])(?:,|$)

() 是一个群组

* 是一个量词

如果在左括号之后有一个?,那么它是一个特殊的组,这里(?&lt;=,|^) 是一个后向断言。

方括号声明一个字符类,例如[^\"]。这个很特别,因为开头是^。它是一个否定字符类。

| 表示交替,即 OR 运算符。

(?:,|$) 是一个非捕获组

$是正则表达式中的一个特殊字符,它是一个锚(匹配字符串的结尾)

【讨论】:

    【解决方案2】:
    "([^\"]*)"|(?<=,|^)([^,]*)(?:,|$)
    
    () capture group
    (?:) non-capture group
    [] any character within the bracket matches
    \ escape character used to match operators aka "
    (?<=) positive lookbehind (looks to see if the contained matches before the marker)
    | either or operator (matches either side of the pipe)
    ^ beginning of line operator
    * zero or more of the preceding character
    $ or \z end of line operator 
    

    为了将来参考,请收藏a good regex reference,它可以很好地解释每个部分。

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多