• 正则使用案例


    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);
    }

    }

  • 相关阅读:
    搭建docker镜像仓库(一):使用registry搭建本地镜像仓库
    C++ quick sort
    Ubuntu C++ uuid_generate vs Windows UuidCreate
    今天做错的笔试题:StringBuffer引用传参
    一般报java.lang.NullPointerException的原因有以下几种
    has a / is a 的区别
    1、一日一程序之C语言的Hanoi问题
    Java enum的用法详解
    OpenGL ES EAGLContext 和 EGLContext
    Windows OpenGL ES 图像对比度调节
  • 原文地址:https://www.cnblogs.com/lwh-12345/p/13367857.html
Copyright © 2020-2023  润新知