dimanche 19 mars 2017

Pattern Matcher Java - Finding All Substrings in a String [duplicate]

This question already has an answer here:

Lets say I have a string "ATATATAT" and I want to use pattern and matcher to find all instances of "ATAT" in the given string. Why do I only get the two outside instances of "ATAT" and not the one in the middle? (ATATATAT) This is my code:

import java.util.regex.*;

public static void main (String args[]){
    String in = "ATATATAT";
    Pattern p = Pattern.compile("ATAT");
    Matcher m = p.matcher(in);
    while(m.find()){
        System.out.println("Start: " + m.start() + " " + End: " + m.end());
    }
}

And the result is:

Start: 0 End: 4
Start: 4 End: 8

Is it a problem with the regex? Thanks!

Aucun commentaire:

Enregistrer un commentaire