Results 1 to 4 of 4
  1. #1
    racha0601 is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Regex Pattern/Matcher - Print only one occurrence!

    This is my code:

    import java.util.regex.*;
    import java.io.*;

    /**
    * Print all the strings that match a given pattern from a file.
    */
    public class ReadIter {
    public static void main(String[] args) throws IOException {
    // The RE pattern

    Pattern patt = Pattern.compile("VID_.{4}");

    // A FileReader (see the I/O chapter)
    BufferedReader r = new BufferedReader(new FileReader("setupapi.log"));

    // For each line of input, try matching in it.
    String line;
    while ((line = r.readLine()) != null) {
    // For each match in the line, extract and print it.
    Matcher m = patt.matcher(line);
    while (m.find()) {
    // Simplest method:
    System.out.println(m.group(0));

    // Get the starting position of the text
    int start = m.start(0);
    // Get ending position
    int end = m.end(0);
    // Print whatever matched.
    // Use CharacterIterator.substring(offset, end);
    System.out.println(line.substring(start, end));
    //System.out.println(line);
    }
    }
    }
    }


    This is my output:

    VID_0930
    VID_0930
    VID_0930
    VID_0930
    VID_0930
    VID_0930
    VID_090C
    VID_090C
    VID_090C
    VID_090C
    VID_090C
    VID_090C
    VID_0A12
    VID_0A12
    VID_0A12
    VID_0A12
    VID_0A12
    VID_0A12
    VID_0A12
    VID_0A12
    VID_0A12
    VID_0A12
    VID_0A17
    VID_0A17
    VID_0A17
    VID_0A17
    VID_0A17
    VID_0A17

    I need to print out only one occurrence of each VID, i.e. 'VID_0A17' is printed 6 times as it appears 6 times in the file. Is there a way I can set the search to find the match only once so there are no duplicates on output?

    Many Thanks

    Rachael

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Easiest way is to put each matched string into a collection instead of printing it. Then you can check whether you've already seen something. A Map will check automatically for you, but a different structure might be more suitable.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    You need a Set, not a Map.

    db

  4. #4
    racha0601 is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Set's

    Hi, Thanks for your responses. Set's do seem to be whar I need! I think have ammended my code to put the values in a set, however its still outputting duplicate values. Perhaps I have not put them in a set correctly? Any advice would be much appreciated:

    import java.util.Set;
    import java.util.LinkedHashSet;
    import java.util.HashSet;
    import java.util.regex.*;
    import java.io.*;
    import java.util.*;

    /**
    * Print all the strings that match a given pattern from a file.
    */
    public class ReadIter2 {
    public static void main(String[] args) throws IOException {

    Pattern patt = Pattern.compile("(VID_.{4})(?!.+VID_.{4})");

    // A FileReader)
    BufferedReader r = new BufferedReader(new FileReader("setupapi.log"));

    // For each line of input, try matching in it.
    String line;
    while ((line = r.readLine()) != null) {
    // For each match in the line, extract and print it.
    Matcher m = patt.matcher(line);

    ArrayList retval = new ArrayList();

    while (m.find()) {
    //String tmp = m.group();
    retval.add(m.group());

    // Create the set
    HashSet set = new HashSet(retval);

    //Set setItems = new LinkedHashSet(list);
    retval.clear();
    retval.addAll(set);

    System.out.println(retval);




    }
    }
    }
    }

Similar Threads

  1. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  2. Replies: 11
    Last Post: 04-16-2008, 01:52 PM
  3. Regex pattern
    By ravian in forum New To Java
    Replies: 4
    Last Post: 12-11-2007, 10:20 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
  •