2015-10-25 104 views
1

是否有可能的方法來查找文本文件行中的特定字符串並將它們存儲在變量中?從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值。

+0

你只是想後@給取字像南阿拉巴馬州。然後閱讀每一行,並通過正則表達式得到值 –

+0

甚至沒有整個名稱,我只是想要什麼附加到@。我如何取詞? – Hector

+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]+) =​​- 第三匹配組,修剪匹配
  • 第二組
  • \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