zoukankan      html  css  js  c++  java
  • 正则使用案例

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

    }

  • 相关阅读:
    Chrome触发唤起IE, 注册唤起程序
    .net post请求过长 , 超过配置 maxQueryStringLength值
    eclipse 初探踩坑实录
    eslint 报error
    前端3小时配置空白机环境
    sql语句
    maven3.04管理jetty9.2.10启动web项目
    Oracle日期时间
    AngularJS向指令传递数据
    jetty和tomcat启动项目
  • 原文地址:https://www.cnblogs.com/lwh-12345/p/13367857.html
Copyright © 2011-2022 走看看