集册 Java实例教程 使用正则表达式查找单词并获取文本中的偏移量

使用正则表达式查找单词并获取文本中的偏移量

欢马劈雪     最近更新时间:2020-01-02 10:19:05

407
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用正则表达式查找单词并获取文本中的偏移量
//from 时 代      J a v a   公   众 号 - nowjava.com


import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class Main {


  private static final String REGEX = "\\bcat\\b";

  private static final String INPUT = "cat cat. cat, cat| cat+ +cat cattie cat";


  public static void main(String args[]) {

    Pattern p = Pattern.compile(REGEX);

    Matcher m = p.matcher(INPUT); // get a matcher object

    int count = 0;/*来 自 N o w J a v a . c o m - 时代Java*/


    if (m.find()) {

      count++;

      System.out.println("----------------------------");

      System.out.println("LookingAt:" + m.lookingAt());

      System.out.println("Match number:" + count);

      System.ou
展开阅读全文