i want to print out the position of the second occurrence of zip in text, or -1 if it does not occur at least twice.
public class UdaciousSecondOccurence {
String text = "all zip files are zipped";
String text1 = "all zip files are compressed";
String REGEX = "zip{2}"; // atleast two occurences
protected void matchPattern1(){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(text);
while(m.find()){
System.out.println("start index p" +m.start());
System.out.println("end index p" +m.end());
// System.out.println("Found a " + m.group() + ".");
}
output for matchPattern1()
start index p18
end index p22
But it does not print anything for pattern text1 - i have used a similar method for second pattern -
text1does not contain the "zipp" sequence?zip{2}will matchzipp"zip"as a sequence and{2}as number of occurrences - now it makes sense, thanks to all the feedback down below.