1

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 -

3
  • 2
    Is it just me or text1 does not contain the "zipp" sequence? Commented Jun 3, 2014 at 10:17
  • your regex is wrong, zip{2} will match zipp Commented Jun 3, 2014 at 10:18
  • @micheal and @heroandtn3 - actually i was trying "zip" as a sequence and {2} as number of occurrences - now it makes sense, thanks to all the feedback down below. Commented Jun 3, 2014 at 10:30

8 Answers 8

1

text1 does not match the regex zip{2}, therefore the while loop never iterates because there are no matches.

The expression is attempting to match the literal zipp, which is contained in text but not text1. regexr

If you want to match the second occurrence, I would recommend using a capture group: .*zip.*?(zip)

Example

    String text = "all zip files are zip";
    String text1 = "all zip files are compressed";

    String REGEX = ".*zip.*?(zip)";
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(text);

    if(m.find()){       
            System.out.println("start index p" + m.start(1));
            System.out.println("end index p" + m.end(1));
    }else{
        System.out.println("Match not found");
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Use the below code it may work for you

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);

    if(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() + ".");

    }else{
        System.out.println("-1");
    }
}

public static void main(String[] args)  {

         UdaciousSecondOccurence uso  = new UdaciousSecondOccurence();
         uso.matchPattern1();
    }   

 }

2 Comments

output is start index1 18 end index1 22 -1 i think it does solve most part of the problem.
Shouldn't your end index be 21 as opposed to 22?
0

If it must match twice, rather than using a while loop I would code it like this using regex "zip" (once, not twice):

if (m.find() && m.find()) {
    // found twice, Matcher at 2nd match
} else {
    // not found twice
}

p.s. text1 doesn't have two zips

2 Comments

Of course you should xearch for just "zip" (one zip, not two)
it works this way - if (m.find() && m.find()){ System.out.println("start index1 " +m.start()); System.out.println("end index1 " +m.end()); } else { System.out.println("-1"); }
0

zip{2} matches the string zipp -- the {2} applies only to the element immediately preceding. 'p'.

That is not what you want.

You probably just want to use zip as your regex, and leave the counting of occurrences to the code around it.

Comments

0

Why don't you just use String.indexOf twice?

String text = "all zip files are zipped";
String text1 = "all zip files are compressed";
int firstOccurrence = text.indexOf("zip");
int secondOccurrence = text.indexOf("zip", firstOccurrence + 1);
System.out.println(secondOccurrence);
firstOccurrence = text1.indexOf("zip");
secondOccurrence = text1.indexOf("zip", firstOccurrence + 1);
System.out.println(secondOccurrence);

Output

18
-1

2 Comments

very nice, i always forgot indexOf method - first time you read java REGEX - it just creates more confusion than solving problems
@kasper_341 yes, sometimes there's easier solutions than regex :)
0

The second time, statements inside while(m.find()) are never executed. because find() will not be able to find any match

Comments

0

You need one or 2 pattern matching. Try with regex zip{1,2},

  String REGEX = "zip{1,2}";

Comments

0

There could be two reasons: 1st: Text1 doesn't contain two 'zip'. 2nd: You need to add the piece of code that would print '-1' upon finding no match. e.g. if m.find = true then print index else print -1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.