在Java中从文本文件检索某些字符串
是否有可能在文本文件的行中找到特定字符串并将其存储在变量中 例如:在Java中从文本文件检索某些字符串,java,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
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...
}
if(secondToken.contains(“@”)&&int1>int2){
东西
}else if(第四个令牌包含(“@”)|第五个令牌包含(“@”)和&int1
我已经存储了行中的所有int值。您可以使用带有以下正则表达式的and类:
^\d{4}-\d{2}-\d{2}\s+([^\d]+)([\d]+)\s+([^\d]+)([\d]+)\s*$
这个正则表达式为每个团队名称和分数都有匹配的组-反斜杠需要在字符串中转义。然后可以循环所有匹配项,并检查匹配组值的起始字符串“@”
给定行:
正则表达式模式将匹配如下:
=\d{4}-\d{2}-\d{2}\s+
“2007-11-11”
=([^\d]+)
-第一个匹配组,修剪匹配“@South Alabama”
=“72”-第二个匹配组([\d]+)
=\s+
“”
=([^\d]+)
-第三组,修剪比赛“山姆休斯顿街”
=“54”-第四个匹配组([\d]+)
=\s*
“”
你只想在“像南阿拉巴马州”之后找词。然后读取每一行并通过regex获取值,甚至不是整个名称,我只需要@上附加的内容。我怎么取单词?你是说整行?你的问题不清楚。文本文件的格式如何请参见编辑。我只是想在每一行上用@来检查这个词。@Hector-但它比这更复杂,不是吗?您是只想要“
South
”,还是想要“South Alabama
”?您打算将此数据格式设置为固定宽度列,还是使用某种类型的分隔符?
2007-11-11 @South Alabama 72 Sam Houston St 54
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