You can use the Pattern and Matcher classes with a regex of:
^\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:
\d{4}-\d{2}-\d{2}\s+ = "2007-11-11 "
([^\d]+) = "@South Alabama " - First matching group, trim the match
([\d]+) = "72" - Second matching group
\s+ = " "
([^\d]+) = "Sam Houston St " - Third matching group, trim the match
([\d]+) = "54" - Fourth matching group
\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
South", or do you want "South Alabama"? Do you intend for this data format to be fixed-width columns, or using some type of delimiter?