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

Java 正则表达式-匹配字符串模式,java,regex,Java,Regex,我想打印出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 v

我想打印出
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() + ".");

        }
matchPattern1()的输出
起始索引p18
结束索引p22


但是它不会为模式
text1
打印任何内容-我对第二个模式使用了类似的方法-

text1
与正则表达式
zip{2}
不匹配,因此while循环不会迭代,因为没有匹配项

表达式试图匹配文本
zip
,该文本包含在
text
中,但不包含
text1

如果要匹配第二个匹配项,我建议使用捕获组:
*zip.*(zip)

示例

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

zip{2}
匹配字符串
zip
——
{2}
仅应用于前面的元素p’

那不是你想要的


您可能只想使用
zip
作为正则表达式,并将出现次数的计数留给它周围的代码。

为什么不只使用
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);
输出

18
-1

第二次,
while(m.find())
中的语句永远不会执行。因为
find()
将无法找到任何匹配

您需要一个或两个模式匹配。尝试使用regex
zip{1,2}
,

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

如果它必须匹配两次,而不是使用while循环,我将使用regex
“zip”
(一次,而不是两次)对其进行如下编码:


p、 美国text1没有两个拉链,可能有两个原因: 第一:Text1不包含两个“zip”。 第二:您需要添加一段代码,在发现不匹配时打印'-1'。e、 g.如果m.find=true,则打印索引
else print-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);

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

 }

是我还是
text1
不包含“zipp”序列?你的正则表达式是错误的,
zip{2}
将匹配
zipp
@micheal和@heroandtn3-事实上,我尝试将
“zip”
作为一个序列和
{2}
作为出现次数-现在它是有意义的,感谢下面的所有反馈。非常好,我总是忘记
indexOf
method——第一次读java正则表达式时——它只会造成比解决问题更多的混乱problems@kasper_341对有时有比正则表达式更简单的解决方案:)输出是
start index1 18 end index1 22-1
我认为它确实解决了大部分问题。你的结束索引不是应该是21而不是22吗?当然你应该搜索“zip”(一个zip,而不是两个)。它是这样工作的-
如果(m.find()&&m.find()){System.out.println(“start index1”+m.start());System.out.println(“end index1”+m.end());}否则{System.out.println(“-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);

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

 }