zoukankan      html  css  js  c++  java
  • 正则表达式中Matcher上的4个方法

    boolean matches()
    boolean lookingAt()
    boolean find()
    boolean find(int start)
    
    • matches()方法用来判断整个输入字符串是否匹配正则表达式模式
    • lookingAt()用来判断该字符串(不必是整个字符串)的起始部分是否能够匹配模式
    • find()用来在CharSequence中查找多个匹配。
    //: strings/Finding.java
    import java.util.regex.*;
    import static net.mindview.util.Print.*;
    
    public class Finding {
      public static void main(String[] args) {
        Matcher m = Pattern.compile("\w+")
          .matcher("Evening is full of the linnet's wings");
        while(m.find())
          printnb(m.group() + " ");
        print();
        int i = 0;
        while(m.find(i)) {
          printnb(m.group() + " ");
          i++;
        }
      }
    } /* Output:
    Evening is full of the linnet s wings
    Evening vening ening ning ing ng g is is s full full ull ll l of of f the the he e linnet linnet innet nnet net et t s s wings wings ings ngs gs s
    *///:~
    

    find()像迭代器那样遍历输入的字符串,而第二个带参的find(int start)则是接受一个参数,该参数表示字符串中字符的位置,并以其为搜索的起点。

  • 相关阅读:
    Selenium(Python)等待元素出现
    java文件的I/O
    Map的四种遍历方式
    模板类实现链表
    字符串相关库函数使用
    动态规划之背包问题
    最长递增子序列
    深度优先搜索(DFS),逃离迷宫
    素数环问题(递归回溯)
    枚举(百鸡问题)
  • 原文地址:https://www.cnblogs.com/fanlumaster/p/13681705.html
Copyright © 2011-2022 走看看