正则表达式创建一个不应存在的空数组

时间:2016-07-20 22:10:14

标签: java regex

我试图从IMDB解析制表符分隔值转储。 (The actual dump在每行中包含不一致的标签数量。):

package com.mycompany.imdbproject;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ActorListParser {

Charset charset = Charset.forName("ISO-8859-1");

BufferedReader reader = null;

public ActorListParser() {

    try {

        this.reader = Files.newBufferedReader(
                new File(System.getProperty("user.home") + ("/IMDBLogs" + "/dataDirectory" + "/acsshort.txt")).toPath(), charset);

        String line = null;

        while ((line = reader.readLine()) != null) {
            String[] lineAsArray = null;

            Pattern startsWithTab = Pattern.compile("^\t.*$");

            Matcher tab = startsWithTab.matcher(line);

            boolean startsWithTabMatcher = tab.matches();

            if (!startsWithTabMatcher) {

                lineAsArray = line.split("\t");

                for (int i = 0;i < lineAsArray.length; i++) {

                    System.out.println("Length: " + lineAsArray.length +", Value:"+ i +"  "+ lineAsArray[i]);
                }
            }else{
            //parse lines that start with a tab (actor's other movies)
            }

        }
    } catch (IOException ex) {

        Logger.getLogger(ActorListParser.class.getName()).log(Level.SEVERE, null, ex);

    }
}

public static void main(String[] args) {
    ActorListParser acp = new ActorListParser();
}

}

我的代码:

Length: 2, Value:0  $, Claw
Length: 2, Value:1          "OnCreativity" (2012)  [Himself]
Length: 1, Value:0  
Length: 2, Value:0  $, Homo
Length: 2, Value:1          Nykytaiteen museo (1986)  [Himself]  <25>
Length: 1, Value:0    
Length: 2, Value:0  $hutter
Length: 2, Value:1          Battle of the Sexes (2017)  (as $hutter Boy)  [Bobby Riggs Fan]  <10>
Length: 1, Value:0  
Length: 2, Value:0  $lim, Bee Moe
Length: 2, Value:1      Fatherhood 101 (2013)  (as Brandon Moore)  [Himself - President, Passages]
Length: 1, Value:0  
Length: 2, Value:0  $ly, Yung
Length: 2, Value:1      Town Bizzness Pt 3 (2014) (V)  [Yung $ly]
Length: 1, Value:0  
Length: 2, Value:0  $torm, Cuntry
Length: 2, Value:1      From the Woods: The Discovery of LYB (????)  (as Country $torm)  [Himself]
Length: 1, Value:0  
Length: 2, Value:0  & Davi, Bruninho
Length: 2, Value:1  Michel na Balada (2011) (V)  [Themselves]
Length: 1, Value:0  
Length: 2, Value:0  & Dollar Furado, Caio Corsalette
Length: 2, Value:1  "Som Brasil" (2007) {Zezà di Camargo & Luciano (#5.7)}  [Themselves]
Length: 1, Value:0  
Length: 2, Value:0  & Fabiano, CÃsar Menotti
Length: 2, Value:1  Nascemos para Cantar (2010) (TV)  [Themselves]

输出:

docker build -t sshable ~/PATH/" and "docker run --name test -t sshable  

正如您所看到的,我首先看到了作者姓名,并从中解析了名称和电影。(以后在地图中使用)。我会在另一个正则表达式中将其他电影归结为演员。

不幸的是,有一个长度为1的数组,没有值继续出现在我的输出中。我做错了什么是创建这个空数组?

1 个答案:

答案 0 :(得分:4)

输入中有空行。这些不以制表符开头,因此它们与if语句匹配。然后,在任何东西上拆分空行将导致长度为1且带有空字符串元素的数组。例如,"".split("blah")返回一个长度为1的数组,其中包含一个空字符串元素。这就是String.split的工作方式。

因此,解决方案是添加!line.isEmpty()

的检查 @Andreas在评论中说得最好:

  

烨。请参阅javadoc:如果表达式与输入的任何部分都不匹配,则生成的数组只有一个元素,即此字符串。