Matcher not finding overlapping words?

前端 未结 4 510
猫巷女王i
猫巷女王i 2021-01-28 08:05

I\'m trying to take a string:

String s = \"This is a String!\";

And return all 2-word pairs within that string. Namely:

{\"this         


        
4条回答
  •  不知归路
    2021-01-28 08:28

    I tried with group of pattern.

    String s = "this is a String";
    
    Pattern pat = Pattern.compile("([^ ]+)( )([^ ]+)");
    Matcher mat = pat.matcher(s);
    boolean check = mat.find();
    while(check){
        System.out.println(mat.group());
        check = matPOS.find(mat.start(3));
    }
    

    from the pattern ([^ ]+)( )([^ ]+)
    ...........................|_______________|
    ..................................group(0)
    ..........................|([^ ]+)| <--group(1)
    ......................................|( )| <--group(2)
    ............................................|([^ ]+)| <--group(3)

提交回复
热议问题