正则表达式(去除括号及括号里面的内容,去除“的”字,去除非中文前缀,去除非中文后缀,自定义规则进行规范数据)

1.去除括号及括号里面的内容

@Test
	public void test1() {
		String oldValue="哈哈哈(不要了)";		
		System.out.println(oldValue.replaceAll("(\\()(.*?)(\\))", ""));
	}

运行结果:
正则表达式(去除括号及括号里面的内容,去除“的”字,去除非中文前缀,去除非中文后缀,自定义规则进行规范数据)_第1张图片

2.去除“的”字结构
(主要是找到“的”字的Unicode码)

@Test
	public void test2() {
		String oldValue="我家的卧室有一台很大的电视";
		
		System.out.println(oldValue.replaceAll("\\u7684", ""));
		
	}

运行结果:
正则表达式(去除括号及括号里面的内容,去除“的”字,去除非中文前缀,去除非中文后缀,自定义规则进行规范数据)_第2张图片

3.去除非中文前缀

@Test
	public void test3() {
		String olaVlaue="zhans1e3张三黄123zhangsan李四色的";
		int position=0;
		for(int i=0;i<olaVlaue.length();i++){
			if(!String.valueOf(olaVlaue.charAt(i)).matches("[\u4e00-\u9fa5]")) {
				position++;
			   continue;
			}else {
				break;
			}
		}
		olaVlaue=olaVlaue.substring(position, olaVlaue.length());
		System.out.println(olaVlaue);
	}

运行结果:
正则表达式(去除括号及括号里面的内容,去除“的”字,去除非中文前缀,去除非中文后缀,自定义规则进行规范数据)_第3张图片

4.去除非中文后缀

@Test
	public void test4() {
		String olaVlaue="张三黄123zhangsan李四色的fafa中华123za";
		int position=0;
		for(int i=olaVlaue.length()-1;i>=0;i--){
			if(!String.valueOf(olaVlaue.charAt(i)).matches("[\u4e00-\u9fa5]")) {
				position++;
			   continue;
			}else {
				break;
			}
		}
		olaVlaue=olaVlaue.substring(0, olaVlaue.length()-position);
		System.out.println(olaVlaue);
	}

运行结果:
正则表达式(去除括号及括号里面的内容,去除“的”字,去除非中文前缀,去除非中文后缀,自定义规则进行规范数据)_第4张图片

5.自定义规则进行规范数据

@Test
	public void test5() {
		
		String regex="[\\u4e00-\\u9fa5]";//自定义正则表达式(可以前端传入)
		String str="dada张三lkdlsl李四";
		Pattern pattern=Pattern.compile(regex);
		Matcher matcher=pattern.matcher(str);
		int count=0;
		String newStr="";
		while(matcher.find()) {
			//start()和end() 代表的是字符串符合正则表达式的字符下标
			 System.out.println("Start():"+matcher.start());
			 System.out.println("end()"+matcher.end());
			newStr+=str.substring(matcher.start(),matcher.end());
			
		}
		System.out.println(newStr);
	}

正则表达式(去除括号及括号里面的内容,去除“的”字,去除非中文前缀,去除非中文后缀,自定义规则进行规范数据)_第5张图片

你可能感兴趣的:(问题解决)