zoukankan      html  css  js  c++  java
  • Java如何重置正则表达式的模式?

    在Java编程中,如何重置正则表达式的模式?

    以下示例演示如何使用PatternPattern.compile()方法和Matcher类的m.find()方法来重置正则表达式的模式。

    package com.yiibai;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class SplittingString {
        public static void main(String[] args) throws Exception {
            Matcher m = Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags");
            while (m.find())
                System.out.println(m.group());
            m.reset("fix the rig with rags");
            while (m.find())
                System.out.println(m.group());
        }
    }
    
    Java

    上述代码示例将产生以下结果 -

    fix
    rug
    bag
    fix
    rig
    rag
    
    Shell

    示例-2

    以下是重新设置正则表达式模式的另一个示例:

    package com.yiibai;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class SplittingString2 {
        public static void main(String args[]) {
            Pattern p = Pattern.compile("\d");
            Matcher mat1 = p.matcher("9652018244");
    
            while (mat1.find()) {
                System.out.println("	" + mat1.group());
            }
            mat1.reset();
            System.out.println("After done resetting the Matcher, it should be like this");
    
            while (mat1.find()) {
                System.out.println("	" + mat1.group());
            }
        }
    }
    
    Java

    上述代码示例将产生以下结果。

        9
        6
        5
        2
        0
        1
        8
        2
        4
        4
    After done resetting the Matcher, it should be like this
        9
        6
        5
        2
        0
        1
        8
        2
        4
        4
    
    Shell
     
  • 相关阅读:
    面向对象、构造函数的区别
    写一个function,清除字符串前后的空格。(兼容所有浏览器)
    两个DIV高度自适应方法(左右两个DIV高度一样)
    js数组去重
    input框处理删除小图标的功能
    查找显示高亮
    JSON.parse()和JSON.stringify()
    jquery封装
    怎么理解HTML语义化
    html5语义化标签
  • 原文地址:https://www.cnblogs.com/borter/p/9617142.html
Copyright © 2011-2022 走看看