kasper_341
kasper_341

Reputation: 95

Regular Expression - Match String Pattern

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 -

Upvotes: 1

Views: 171

Answers (8)

Ramesh Boddepalli
Ramesh Boddepalli

Reputation: 301

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

 }

Upvotes: 1

Bohemian
Bohemian

Reputation: 425448

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

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

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

Upvotes: 1

Adnan Nazir
Adnan Nazir

Reputation: 86

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

Upvotes: 0

Masudul
Masudul

Reputation: 21981

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

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

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

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

Upvotes: 0

Mena
Mena

Reputation: 48444

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

Upvotes: 0

slim
slim

Reputation: 41271

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.

Upvotes: 0

Related Questions