从Java中的文本文件中检索某些字符串

时间:2015-10-25 02:52:38

标签: java

是否有可能在文本文件的行中查找特定字符串并将其存储在变量中?

例如:

2007-11-11 @South Alabama            72  Sam Houston St           54           
2007-11-11 Auburn                    91  Detroit                  47           
2007-11-11 @Missouri KC              65  Air Force                59           
2007-11-11  Ga Southern              67 @Stetson                  51   

对于这个文本文件,我想存储其中带有@符号的字符串,以便稍后我可以使用String.contains在程序中运行测试。

换句话说,有没有办法获得第二个,第四个或第五个字符串,并检查它们是否有@符号?

我想做类似的事情:

if(secondToken.contains("@") && int1 > int2){
stuff...
}else if(fourthToken.contains("@") || fifthToken.contains("@") && int1 < int2){
other stuff...
}

我已经存储了行中的所有int值。

1 个答案:

答案 0 :(得分:1)

您可以将PatternMatcher类与正则表达式一起使用:

  

^\d{4}-\d{2}-\d{2}\s+([^\d]+)([\d]+)\s+([^\d]+)([\d]+)\s*$

此正则表达式具有每个团队名称和分数的匹配组 - 反斜杠需要在字符串中进行转义。然后,您可以循环遍历所有匹配项,并检查匹配的组值是否为“@”的起始字符串。

鉴于以下行:

2007-11-11 @South Alabama            72  Sam Houston St           54           

正则表达式模式将匹配如下:

  • \d{4}-\d{2}-\d{2}\s+ = "2007-11-11 "
  • ([^\d]+) = "@South Alabama " - 第一个匹配组, 修剪匹配
  • ([\d]+) = "72" - 第二个匹配组
  • \s+ = " "
  • ([^\d]+) = "Sam Houston St " - 第三个匹配组, 修剪匹配
  • ([\d]+) = "54" - 第四个匹配组
  • \s* = " "

示例代码:

public static void main(String[] args) {
    // Input string with \n newlines
    String input = "2007-11-11 @South Alabama            72  Sam Houston St           54           \n2007-11-11 Auburn                    91  Detroit                  47          \n2007-11-11 @Missouri KC              65  Air Force                59           \n2007-11-11  Ga Southern              67 @Stetson                  51   ";
    // The regex with four matching groups, backslashes escaped
    String regex = "^\\d{4}-\\d{2}-\\d{2}\\s+([^\\d]+)[\\d]+\\s+([^\\d]+)[\\d]+\\s*$";
    // Pattern with multiline and insensitive options
    Pattern p = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE );
    // Match each line in the input
    Matcher m = p.matcher(input);
    // Loop over the matches
    while (m.find()){
        // first team name
        String first = m.group(1).trim();
        // first team score
        Integer firstScore = new Integer(m.group(2).trim());
        // second team name
        String second = m.group(3).trim();
        // second team score
        Integer secondScore = new Integer(m.group(4).trim());

        // is the first team the home team?
        if ( first.startsWith("@") ){
            // did they win?
            if (firstScore > secondScore){
                System.out.println("The home team " + first + " won " + firstScore + " to " + secondScore + " over " + second);
            } else {
                System.out.println("The home team " + first + " lost to " + second + " " + firstScore + " to " + secondScore);
            }
        } else 
        // is the second team the home team?
        if ( second.startsWith("@") ){
            // did they win?
            if (secondScore > firstScore){
                System.out.println("The home team " + second + " won " + secondScore + " to " + firstScore + " over " + first);
            } else {
                System.out.println("The home team " + second + " lost to " + first + " " + secondScore + " to " + firstScore);
            }
        } else {
            System.out.println("Both teams - " + first + " and " + second + " - were away.");
        }
    }
}

示例输出:

The home team @South Alabama won 72 to 54 over Sam Houston St
Both teams - Auburn and Detroit - were away.
The home team @Missouri KC won 65 to 59 over Air Force
The home team @Stetson lost to Ga Southern 51 to 67