zoukankan      html  css  js  c++  java
  • java 正则表达式例子, 查找字符串

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class Main {

    public static void main(String[] args) throws Exception {
    String str = "10.2368686986859686";
    Pattern p = Pattern.compile("[\d]*[\.][\d]{2}"); // 小数保留两位小数
    Matcher m = p.matcher(str);

    // 查找相应的字符串
    while (m.find()) {
    String tmp = m.group();
    if (!"".equals(tmp)) {
    System.out.println(tmp);
    }
    }

    // 判断是否匹配
    System.out.println(m.matches());

    str = "上山打老虎打不到";
    p = Pattern.compile("老虎打不到");
    m = p.matcher(str);

    // 查找相应的字符串
    while (m.find()) {
    String tmp = m.group();
    if (!"".equals(tmp)) {
    System.out.println(tmp);
    }
    }
    }
    }

  • 相关阅读:
    E
    D
    C
    B
    Python
    IDEA 设置资源目录
    光猫指示灯含义
    IO模型
    Linux 总目录
    Linux python 使用
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/4583753.html
Copyright © 2011-2022 走看看