zoukankan      html  css  js  c++  java
  • 对字符串的查找,剪切,替换,提取(正则表达式)

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

    //对字符串的查找,剪切,替换,提取
    public class StringAction {
        public static void main(String[] args) {
            StringAction sa = new StringAction();
            System.out.print(sa.action4());
        }

        public Boolean action1() {// 查询,判断某个字符串里是否有这个字符串
            String str = "abcdefgh";
            String regex = "a";
            Pattern p = Pattern.compile(regex);
            // 如果想在查找时忽略大小写,则可以写成pattern     p=pattern.compile(regex,pattern.case_insensitive);
            Matcher m = p.matcher(str);
            return m.find();
        }

        public String[] action2() {// 将一个字符串根据某个特定符号,生成数组
            String str = "xd::abc::cde";
            return str.split("::");
        }

        public String action3() {// 将一个字符串里一个或多个a的地方替换为一个a
            String regex = "a+"; // 表示一个或多个a
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher("aaabbced a ccdeaa");
            return m.replaceAll("a");
        }

        public String action4() {// 提取
            String regex = ".+\\(.+)$";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher("c:\dir1\dir2\name.txt\a");
            if (m.find()) {
                return m.group(1);
            }
            return null;
        }
    }

  • 相关阅读:
    numpy之数组属性与方法
    numpy之数组创建
    matplotlib之直方图
    matplotlib之条形图
    matplotlib之散点图
    matplotlib之折线图
    Kettle使用教程之数据同步
    Kettle使用教程之Job使用
    Kettle使用教程之安装与资源库的创建
    Ubuntu16.04配置单机版Zookeeper和Kafka
  • 原文地址:https://www.cnblogs.com/mcahkf/p/4760317.html
Copyright © 2011-2022 走看看