• 正则使用案例


    package com.shine.eiuop.utils;

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

    public class ZWUtils {

    public static void main(String[] args) {

    //String str = " 业务需要 现由 ";
    //String str = " 业务需要 现由ggsad撒旦发广告 ";
    //String str = " 业务需要,现由ggsad撒旦发广告 ";
    //String str = " 选择省/市";
    //String str = " 请输入总行+支行名称 ";
    //String str = "元(人民币)";
    //String str = "大写金额:人民币零圆整";
    String str = "去掉此勾选,可手工编辑摘要";
    String regex = "[u4e00-u9fa5|]";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(str);
    StringBuilder tmSr = new StringBuilder();
    int tmp = -1;

    while (matcher.find()) {
    MatchResult result = matcher.toMatchResult();
    int start = result.start();
    int end = result.end();
    if(tmp == start || tmp == -1) {
    // 判断连续
    tmSr.append(str.substring(start, end));
    }else {
    // 不连续
    //空格,逗号,nbsp;,斜杠,
    if(str.substring(tmp, start).matches("^\s*") //空格
    || str.substring(tmp, start).matches(",(.*)") //英文逗号
    || str.substring(tmp, start).matches(",(.*)") //汉字逗号
    || str.substring(tmp, start).matches(" (.*)")//nbsp;
    || str.substring(tmp, start).matches("/(.*)")//斜杠
    || str.substring(tmp, start).matches("\+(.*)")//加号
    || str.substring(tmp, start).matches("\((.*)")//(
    || str.substring(tmp, start).matches("(.*)\)")
    || str.substring(tmp, start).matches(":(.*)")//:
    ) {
    if (str.substring(tmp, start).matches(" (.*)")) {
    tmSr.append("");
    }else {
    tmSr.append(str.substring(tmp, start));
    }
    }else {
    tmSr.append(" ");
    }
    tmSr.append(str.substring(start, end));
    }
    tmp = end;
    }
    if (str.contains("(")) {
    tmSr.append(")");
    }

    System.out.println(tmSr);
    }

    }

  • 相关阅读:
    Dart中的类型转换总结:
    【Dart学习】--Dart之数组(List)的相关方法总结
    Navigator的使用:
    001——Angular环境搭建、运行项目、搭建项目
    Dart中的数据类型转换:
    Flutter中的Stack、Align、Positioned的使用
    Flutter设置图片为正方形
    顶部导航TabBar、TabBarView、DefaultTabController
    《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <二> NSMutableString
    Swift随记
  • 原文地址:https://www.cnblogs.com/lwh-12345/p/13367857.html
Copyright © 2020-2023  润新知