正则使用案例

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

}

原文地址:https://www.cnblogs.com/lwh-12345/p/13367857.html