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

标签 java regex

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

$, Claw         "OnCreativity" (2012)  [Himself]

$, Homo         Nykytaiteen museo (1986)  [Himself]  <25>
                   Suuri illusioni (1985)  [Guests]  <22>

$hutter         Battle of the Sexes (2017)  (as $hutter Boy)  [Bobby Riggs Fan]  <10>
                   NVTION: The Star Nation Rapumentary (2016)  (as $hutter Boy)  [Himself]  <1>
                   Secret in Their Eyes (2015)  (uncredited)  [2002 Dodger Fan]
                   Steve Jobs (2015)  (uncredited)  [1988 Opera House Patron]
                   Straight Outta Compton (2015)  (uncredited)  [Club Patron/Dopeman]

$lim, Bee Moe       Fatherhood 101 (2013)  (as Brandon Moore)  [Himself - President, Passages]
                   For Thy Love 2 (2009)  [Thug 1]
                   Night of the Jackals (2009) (V)  [Trooth]
                   "Idle Talk" (2013)  (as Brandon Moore)  [Himself]
                   "Idle Times" (2012) {(#1.1)}  (as Brandon Moore)  

$ly, Yung           Town Bizzness Pt 3 (2014) (V)  [Yung $ly]
                   "From Tha Bottom 2 Tha Top" (2016)  [Yung $ly]
                   "From Tha Bottom 2 Tha Top" (2016) {T-Pain (#1.2)}  [Yung $ly]

$torm, Cuntry       From the Woods: The Discovery of LYB (????)  (as Country $torm)  [Himself]

& Davi, Bruninho   Michel na Balada (2011) (V)  [Themselves]
                   Michel TelÛ: Sunset (2013) (V)  [Themselves]
                   "Programa da Sabrina" (2014) {(2016-01-23)}  [Themselves]

& Dollar Furado, Caio Corsalette    "Som Brasil" (2007) {ZezÈ di Camargo & Luciano (#5.7)}  [Themselves]

& Fabiano, CÈsar Menotti    Nascemos para Cantar (2010) (TV)  [Themselves]
                            Show da Virada (2011) (TV)  [Themselves - Performers]
                            Teleton 2010 (2010) (TV)  [Themselves]
                            "Altas Horas" (2000) {(2013-06-29)}  [Themselves]
                            "Altas Horas" (2000) {(2013-12-14)}  [Themselves]
                            "Eliana" (2009) {(2012-10-21)}  [Themselves]
                            "Tudo … PossÌvel" (2005) {(2008-04-13)}  [Themselves]
                            "TV Xuxa" (2005) {(2013-01-05)}  [Themselves]

我的代码:

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]

如您所见,我获取了作者姓名的第一次出现,并从中解析出姓名和电影。(以便稍后在 map 中使用)。我将在单独的正则表达式中获取归因于该 Actor 的其他电影。

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

最佳答案

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

因此,解决方案是添加对 !line.isEmpty() 的检查。

@Andreas 在评论中说得最好:

Yup. See javadoc: If the expression does not match any part of the input then the resulting array has just one element, namely this string.

关于java - 正则表达式正在创建一个不应该存在的空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38491698/

相关文章:

regex - matchstr 与 vimscript 中的正则表达式不匹配

regex - 尽可能快地从众多正则表达式中选择正确正则表达式的算法

javascript - 在逗号后跟任意数字后拆分字符串

Java正则表达式查找特定文件

java - 使用列表的一部分进行比较时的自定义 hashCode

java - Android 使用日期选择器之外的数据

jquery - 如何使用 jquery 匹配标签后的文本?

iphone - 从正则表达式匹配组中删除尾随空格

java - 如何在 Vaadin 中创建移动就绪布局?

java - 输入 ANTLR4 没有可行的替代方案