简体   繁体   English

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

[英]Retrieving certain strings from a text file in Java

Is there a possible way to find particular strings in lines of a text file and store them in variables? 是否有可能在文本文件的各行中查找特定的字符串并将其存储在变量中?

For example: 例如:

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   

For this text file, I would like to store the strings that have an @ symbol in them so that I can run a test later on in the program with String.contains. 对于此文本文件,我想存储其中带有@符号的字符串,以便以后可以在程序中使用String.contains运行测试。

In other words, is there way to get the either the second or the fourth or the fifth string and check if they have an @ symbol? 换句话说,有没有办法获取第二个或第四个或第五个字符串并检查它们是否具有@符号?

I want to do something like: 我想做类似的事情:

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

I already have all the int values in the lines stored. 我已经存储了所有int值。

You can use the Pattern and Matcher classes with a regex of: 您可以将PatternMatcher类与以下正则表达式一起使用:

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

This regex has matching groups for each of the team names and scores - the backslashes will need to be escaped in your string. 此正则表达式为每个团队名称和得分都有匹配的组-需要在字符串中转义反斜杠。 You can then loop over all the matches and check the matching group values for a start string of "@". 然后,您可以遍历所有匹配项,并检查匹配组的值以查找“ @”的起始字符串。

Given the line: 鉴于这一行:

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

The regex pattern will match as follows: regex模式将匹配如下:

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

Example code: 示例代码:

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.");
        }
    }
}

Example output: 输出示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM