利用正则表达式 对字符串进行操作(截取、提取...)
发布时间:2020-12-14 00:59:45 所属栏目:百科 来源:网络整理
导读://提取数字public String numberintercept(String num) {String phoneString =num;Pattern pattern = Pattern.compile("[^0-9]");Matcher matcher = pattern.matcher(phoneString);String all = matcher.replaceAll("");return Pattern.compile("[^0-9]").ma
//提取数字 public String numberintercept(String num) { String phoneString =num; Pattern pattern = Pattern.compile("[^0-9]"); Matcher matcher = pattern.matcher(phoneString); String all = matcher.replaceAll(""); return Pattern.compile("[^0-9]").matcher(phoneString).replaceAll(""); }
<span style="white-space:pre"> </span>//提取汉字 public String intercept(String str) { String regex = "[u4E00-u9FA5]+";// [//u4E00-//u9FFF]为汉字 Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); while (matcher.find()) { sb.append(matcher.group()); } String content = sb.toString(); return content; } <span style="white-space:pre"> </span>//过滤不想要的特殊符号 public String con (String str) { String regEx="[`~!@#$%^&*()+=|{}:;[].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); String url = m.replaceAll("").trim(); return url; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |