zoukankan      html  css  js  c++  java
  • java正則表達式 match、find匹配位置

    如题。对于java正則表達式这几个方法匹配一次后的,匹配位置搞不太清楚,就写了几个样例。例如以下:

      String ss="ooaaoo";
            Pattern pt=Pattern.compile("(o+)");
            Matcher mt=pt.matcher(ss);
           // mt.lookingAt();
           // mt.matches();
            while(mt.find()){
            	System.out.println(mt.group(1)+"|||"+mt.start());
            }

    这个结果非常明显会匹配二次,一次在0位置,一次在4位置。

    看以下的代码

      String ss="ooaaoo";
            Pattern pt=Pattern.compile("(o+)");
            Matcher mt=pt.matcher(ss);
               mt.lookingAt();
           // mt.matches();
            while(mt.find()){
            	System.out.println(mt.group(1)+"|||"+mt.start());
            }
    当我们把matches或lookingat方法之中的一个的凝视拿掉之后,仅仅会发生一次匹配,就是在4位置。

    再看以下的代码:

    String ss="aaooaaoo";
            Pattern pt=Pattern.compile("(o+)");
            Matcher mt=pt.matcher(ss);
              mt.lookingAt();
           // mt.matches();
            while(mt.find()){
            	System.out.println(mt.group(1)+"|||"+mt.start());
            }
    我们的输入字符串ss发生了变化。

    这个程序结果会发生二次匹配,一次在2位置,一次在4位置。

    所以可得出下面结论:

    1.当我们的输入字符串ss开头不匹配正則表達式的时候,matches和lookingat都不影响下次匹配位置。

    2.假设输入字符串开头匹配正則表達式。调用matches或lookingat之后。下一次匹配的位置。会在去掉开头匹配的字符串之后。



  • 相关阅读:
    每个部门都有自己的游戏规则
    ssh作为代理,反向登录没有固定公网ip的局域网内的某远程服务器
    x11vnc 作为远程桌面服务器时vnc客户端键盘无法长按连续输入字符
    vim 编译使用ycm启动问题 fixed
    ubuntu设置普通用户也能执行docker命令
    git常见使用
    切图的必要步骤
    css居中
    清除浮动
    Spring-AOP(2)
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6752445.html
Copyright © 2011-2022 走看看