Results 1 to 5 of 5
Thread: Using regexp
- 11-05-2010, 04:17 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Using regexp
Hello all! I'm new to the forums, my name is Matt.
I've just finished one of my assignments, which requires me to take a text file and output the results in a certain format.
I need to only output text that matches this format in a txt file:
Stoke City : West Bromwich Albion : 5 : 4
An example of an invalid format is:
Hull : :2:3
I've used regexp, matcher and pattern to achieve this (which I'm very new to) but I can only get it to work with this piece of code:
Java Code:public static final Pattern p = Pattern.compile("^([\\w\\s!\"#$%&'()*+,.;<=>?@/\\^_`{|}~-]+):([\\w\\s!\"#$%&'()*+,.;<=>?@/\\^_`{|}~-]+):\\s*([\\d]+)\\s*:\\s*([\\d]+)\\s*$");
I would gratefully appreciate any help you can all offer me :)
Thank you
Matt
- 11-05-2010, 04:22 AM #2
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
thats pretty short already :)
- 11-05-2010, 04:25 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Oh dear haha, I thought it was long. You can tell I'm new to Java.
I'm just worrying because I have to demonstrate this to my tutor tomorrow and that's the only bit I can't properly explain. I think I might have to read the character references over and over.
- 11-05-2010, 04:33 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
have you..uhh.. looked at your code in your first post?
maybe check that out and you'll see why it's short
- 11-05-2010, 04:36 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Oh right, I didn't post the full code because I didn't think it was necessary.
It's only the pattern that I wish to shorten.
The full code is:
Java Code:import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { //defines the pattern that the results must match public static final Pattern p = Pattern.compile("^([\\w\\s!\"#$%&'()*+,.;<=>?@/\\^_`{|}~-]+):([\\w\\s!\"#$%&'()*+,.;<=>?@/\\^_`{|}~-]+):\\s*([\\d]+)\\s*:\\s*([\\d]+)\\s*$"); //Output holder public static final ArrayList<String> output = new ArrayList<String>(); /** * * @param args the command line arguments * * @throws URISyntaxException * @throws FileNotFoundException * @throws IOException */ public static void main(String[] args) throws FileNotFoundException { // Opens the file Scanner s = new Scanner(new File("results.txt")); // Creates an input string String line; // Creates an integer for valid matches int valid = 0; // Creates an integer for invalid matches int invalid = 0; // Creates an integer for invalid goals scored int goals = 0; // Reads the lines of text while (s.hasNext() ) { // Where there is more text, reads the lines line = s.nextLine(); // Trims the white space line = line.trim(); Matcher m = p.matcher(line.trim()); if (m.matches() && !m.group(1).trim().isEmpty() && !m.group(2).trim().isEmpty()) { // If the line matches the pattern increase the count of valid matches valid++; // Work out the sum of goals based on valid matches goals += Integer.parseInt(m.group(3)) + Integer.parseInt(m.group(4)); // Formats the output using stringers and integers output.add(String.format("%s [%d] | %s [%d]", m.group(1).trim(), Integer.parseInt(m.group(3)), m.group(2).trim(), Integer.parseInt(m.group(4)))); } else { // If the string was invalid, increase the count invalid++; } } // Output formatted records for (String result : output) { System.out.println(result); } // Had issues with the count of invalid matches, this sum was the only way to make it work int invalid1 = (invalid - valid) + 2; // Output the valid match statistics System.out.printf("\nValid Match count was %d, total goals scored were %d.\n", valid, goals); //Outputs the invalid match statistics System.out.printf("Invalid Match Count was %d\n", invalid1); } }
Sunderland : Blackburn Rovers : 1 : 2
Similar Threads
-
RegExp and UTF-8 Characters
By Dan0100 in forum New To JavaReplies: 11Last Post: 08-14-2010, 05:26 PM -
RegExp to remove tag from html file with exceptions
By Daedalus in forum Advanced JavaReplies: 3Last Post: 09-27-2008, 05:43 AM -
[SOLVED] help with RegExp
By JT4NK3D in forum New To JavaReplies: 5Last Post: 05-23-2008, 05:05 AM
Bookmarks