集册 Java实例教程 正则表达式搜索程序模板

正则表达式搜索程序模板

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

536
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
正则表达式搜索程序模板
import java.util.regex.Matcher;
/** 
 来自 N  o w  J a v a . c o m**/

import java.util.regex.Pattern;


public class Main {

  public static void main(String[] args) {

    // Compile regular expression

    String patternStr = "b";

    Pattern pattern = Pattern.compile(patternStr);


    // Determine if pattern exists in input

    CharSequence inputStr = "a b c b";

    Matcher matcher = pattern.matcher(inputStr);

    boolean matchFound = matcher.find(); 

    System.out.println(matchFound);

    /**
     * nowjava.com - 时代Java 提 供 
    **/

    // Get matching string

    String match = matcher.group(); // b

    System.out.println(match);


    // Get indices of matching string

    int start = matcher.start(); // 2

    System.out.println(start);

    int end = matcher.end(); // 3

    System.out.println(end);

    
展开阅读全文