简体   繁体   中英

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 -

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

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

 }

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.

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

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

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

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

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
}

ps text1 doesn't have two zips

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. eg if m.find = true then print index else print -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM