代码之家  ›  专栏  ›  技术社区  ›  Bernie Perez

带数字的csv正则表达式

  •  0
  • Bernie Perez  · 技术社区  · 14 年前

    我正在寻找一些正则表达式来帮助解析我的csv文件。

    文件有行

    number,number
    number,number
    Comment I want to skip
    number,number
    number,number
    

    前任:

    319,5446
    564425,87
    Text to skip
    27,765564
    

    我把每一行都读成一个字符串,并希望使用一些常规的express来确保行与(数字,数字)的模式匹配。如果没有,那就不要用这条线。

    4 回复  |  直到 6 年前
        1
  •  4
  •   mbeckish    14 年前

    以下是一个解决方案:

    ^ d+,d+$

        2
  •  1
  •   Mikael Svenson    14 年前

    这应该与您的号码和行开始/结束匹配:

    ^\d+,\d+$
    
        3
  •  0
  •   Kyra    14 年前

    你想用哪种语言做这个? 在Perl中:

    read in each line of the csv file and save to $line
    if($line !~ m/\d+, \d+/) {
        Do whatever you are going to do with the line with 2 numbers
    } 
    

    在Java中的使用 this 网站帮助:

     read in each line of the csv file and save to variable line
     Pattern p = Pattern.compile("\d+, \d+"); // remove space if it isn't in file
     Matcher m = p.matcher(line);
     if (m.find()) {
        //it matches
     }
    
        4
  •  0
  •   Nhat Nguyen    6 年前

    一般来说,您可以使用下面的regexp作为任意数字位数的逗号分隔列表:

    ^\d+(,\d+)*$
    

    希望这有帮助。