简体   繁体   English

正则表达式-匹配字符串模式

[英]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. 我想在text打印出第二次出现zip的位置,如果至少出现两次则为-1。

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 matchPattern1()输出起始索引p18结束索引p22

But it does not print anything for pattern text1 - i have used a similar method for second pattern - 但它不会为模式text1打印任何内容-我对第二个模式使用了类似的方法-

text1 does not match the regex zip{2} , therefore the while loop never iterates because there are no matches. text1与regex zip{2}不匹配,因此while循环永远不会迭代,因为没有匹配项。

The expression is attempting to match the literal zipp , which is contained in text but not text1 . 该表达式尝试匹配文本zipp ,该text包含在text但不包含在text1 regexr regexr

If you want to match the second occurrence, I would recommend using a capture group: .*zip.*?(zip) 如果要匹配第二个匹配项,我建议使用捕获组: .*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. zip{2}与字符串zipp匹配- {2}仅适用于紧接在前的元素。 'p'. '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. 您可能只想使用zip作为您的正则表达式,并将出现次数留在它周围的代码中。

Why don't you just use String.indexOf twice? 为什么不只使用String.indexOf两次?

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. 第二次, while(m.find())内部的语句永远不会执行。 because find() will not be able to find any match 因为find()将无法找到任何匹配项

You need one or 2 pattern matching. 您需要一两个模式匹配。 Try with regex zip{1,2} , 尝试使用正则表达式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): 如果它必须匹配两次,而不是使用while循环,我将使用regex "zip" (一次,不是两次)来像这样编码:

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

ps text1 doesn't have two zips ps text1没有两个拉链

There could be two reasons: 1st: Text1 doesn't contain two 'zip'. 可能有两个原因:第一:Text1不包含两个“ zip”。 2nd: You need to add the piece of code that would print '-1' upon finding no match. 第二:您需要添加找不到匹配项时将打印“ -1”的代码。 eg if m.find = true then print index else print -1 例如,如果m.find = true,则打印索引,否则打印-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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