Results 1 to 5 of 5

Thread: Using regexp

  1. #1
    mattpyle is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default 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'm hoping of some way to shorten this, as it was a friend who gave me this and I'm sure I can cut it down somehow.
    I would gratefully appreciate any help you can all offer me :)
    Thank you
    Matt

  2. #2
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    thats pretty short already :)

  3. #3
    mattpyle is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    have you..uhh.. looked at your code in your first post?

    maybe check that out and you'll see why it's short

  5. #5
    mattpyle is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    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);
        }
    }
    And an example of a valid result in the text file is:
    Sunderland : Blackburn Rovers : 1 : 2

Similar Threads

  1. RegExp and UTF-8 Characters
    By Dan0100 in forum New To Java
    Replies: 11
    Last Post: 08-14-2010, 05:26 PM
  2. RegExp to remove tag from html file with exceptions
    By Daedalus in forum Advanced Java
    Replies: 3
    Last Post: 09-27-2008, 05:43 AM
  3. [SOLVED] help with RegExp
    By JT4NK3D in forum New To Java
    Replies: 5
    Last Post: 05-23-2008, 05:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •