微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式子收集

参考链接:http://www.cnblogs.com/yirlin/archive/2006/04/12/373222.html 1.public static void main(String[] args) { String s = "GET /index.html HTTP/1.1";//字符串s由“GET”、“/index.html”和“HTTP/1.1”组成,中间有一个或多个空格 String tt[] = s.split("\\s{1,}");//按照空格分割字符串,多个空格作为一个空格对字符串进行分割 for(String str: tt)//增强的for循环 System.out.println(str);//输出:GET // /index.html // HTTP/1.1 String qq = s.replaceAll(" {2,}"," ");//把字符串s中的多个空格替换为一个空格 System.out.println(qq);//输出:GET /index.html HTTP/1.1 System.out.println(s);//输出:GET /index.html HTTP/1.1 } } 注解:"\\s{1,}",\s 表示空格 \* 表示 * 2. 截取间的********* String str = "声音通道,你好**********声音通道,你好************你好"; String tt[] = str.split("[^\\*]+");// 3.判断包含URLhttp://blog.csdn.net/ty0415/article/details/7974887 public static void main(String[] args) { // Todo Auto-generated method stub String strContent = "请到以下地址查询查询地址:充值地址:https://pay.sdo.com/Index.aspx?type=card尊敬的用户: 您好,该卡帐号和密码是正确的,请到以下https://pay.sdo.com/Index.aspx?type=card 地址查询查询地址:充值地址:https://pay.sdo.com/Index.aspx?type=card "; String regex = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(strContent); StringBuffer result = new StringBuffer(); while (matcher.find()) { String urlStr = matcher.group(); StringBuffer replace = new StringBuffer(); replace.append("<a href=\"").append(urlStr); replace.append("\" target=\"_blank\">" + urlStr + "</a>"); matcher.appendReplacement(result,replace.toString()); } matcher.appendTail(result); System.out.println(result); }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐