美文网首页
6、常见的匹配

6、常见的匹配

作者: Shelton_Kevin | 来源:发表于2016-04-28 16:42 被阅读15次
1、邮箱的匹配

String mail = "asdf@sina.com"
String reg = "\\w+@\\w+(\\.\\w+)+";
mail.matches(reg);

2、ip地址排序

String ip = "192.168.68.23 102.43.46.66 10.2.45.3 2.2.2.2 8.10.22.1"
ip.replaceAll("(\\d+)","00$1");
ip.replaceAll("0(\\d{3})","$1");
String arr[] = ip.split(" ");
arr.sort();
arr.replaceAll("0
(\\d+)","$1")

3、网页爬虫--获取网页的邮箱地址

Url url = new URL("http://192.168.1.254");
URLConnection conn = url.openConnection();
BufferedReader buf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line =null;
String reg = "\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(reg);
while((line=buf.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find){
System.out.println(m.group);
}}

相关文章

网友评论

      本文标题:6、常见的匹配

      本文链接:https://www.haomeiwen.com/subject/garhrttx.html