samedi 9 avril 2016

Getting overlapping patterns using regex Java

this code is to extract sequential letters from a data-set

import java.util.regex.*;

public class IFS {

    public static void main(String[] args) {

        String a;
        a = "ABC1abc";

        regexchecker ("\\D+", a);
    }

    public static void regexchecker(String theRegex, String stuffToCheck) {
        // compiling the regex pattern
        Pattern checkRegex = Pattern.compile(theRegex);
        // the regex matcher being joined to the pattern
        Matcher regexmatcher = checkRegex.matcher(stuffToCheck);

        int end = stuffToCheck.length();
        for (int i = 0; i < end; i = i + 1) {

            for (int j = i + 1; j <= end; ++j) {

                regexmatcher.region(i, j);
                while (regexmatcher.find()) {
                    if (regexmatcher.group().length() != 0) {         

                        System.out.println(regexmatcher.group());
                    }
                }    
            }
        }
    }
}

OK so I understand that my code will iterate from j to end EVERY TIME but I need it to skip an iteration that gives the same out put.

my output is

  1. A
  2. AB
  3. ABC
  4. ABC
  5. ABC a
  6. ABC ab
  7. ABC abc

and so on when I want an output like

  1. A
  2. B
  3. C
  4. a
  5. b
  6. c
  7. AB
  8. BC
  9. ab
  10. bc
  11. ABC
  12. abc

Any help is much appreciated. My original data-set is much larger than this but I have used a 7 character set for simplicity

Aucun commentaire:

Enregistrer un commentaire