加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式子收集

发布时间:2020-12-14 01:29:58 所属栏目:百科 来源:网络整理
导读:参考链接: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[]
参考链接: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); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读