正则表达式,网页爬虫

前端之家收集整理的这篇文章主要介绍了正则表达式,网页爬虫前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package cn.itcast.regex.demo;
  2.  
  3.  
  4. /*
  5. * 正则表达式。
  6. *
  7. * 正则表达式用于操作字符串数据。
  8. * 通过一些特定的符号来体现的。
  9. * 所以我们为了掌握正则表达式,必须要学习一些符号。
  10. *
  11. * 虽然简化了,但是阅读性差。
  12. *
  13. *
  14. *
  15. */
  16. public class RegexDemo {
  17.  
  18. /**
  19. * @param args
  20. */
  21. public static void main(String[] args) {
  22.  
  23. String qq = "123k4567";
  24. // checkQQ(qq);
  25. String regex = "[1-9][0-9]{4,14}";//正则表达式。
  26. // boolean b = qq.matches(regex);
  27. // System.out.println(qq+":"+b);
  28. // String str = "aoooooooob";
  29. // String reg = "ao{4,6}b";
  30. // boolean b = str.matches(reg);
  31. // System.out.println(str+":"+b);
  32. }
  33. /*
  34. * 需求:定义一个功能对QQ号进行校验。
  35. * 要求:长度5~15. 只能是数字, 0不能开头
  36. */
  37. public static void checkQQ(String qq){
  38. int len = qq.length();
  39. if(len>=5 && len<=15){
  40. if(!qq.startsWith("0")){
  41. try {
  42. long l = Long.parseLong(qq);
  43. System.out.println(l+":正确");
  44. }catch(NumberFormatException e){
  45. System.out.println(qq+":含有非法字符");
  46. }
  47. }else{
  48. System.out.println(qq+":不能0开头");
  49. }
  50. }else{
  51. System.out.println(qq+":长度错误");
  52. }
  53. }
  54.  
  55. }


  1. package cn.itcast.regex.demo;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class RegexDemo2 {
  7.  
  8. /**
  9. * @param args
  10. */
  11. public static void main(String[] args) {
  12.  
  13. /*
  14. * 正则表达式对字符串的常见操作:
  15. * 1,匹配。
  16. * 其实使用的就是String类中的matches方法
  17. *
  18. * 2,切割。
  19. * 其实使用的就是String类中的split方法
  20. *
  21. * 3,替换。
  22. * 其实使用的就是String类中的replaceAll()方法
  23. *
  24. * 4,获取
  25. *
  26. */
  27. functionDemo_4();
  28. }
  29. /*
  30. * 获取
  31. * 将正则规则进行对象的封装。
  32. * Pattern p = Pattern.compile("a*b");
  33. * //通过正则对象的matcher方法字符串相关联。获取要对字符串操作的匹配器对象Matcher .
  34. * Matcher m = p.matcher("aaaaab");
  35. * //通过Matcher匹配器对象的方法对字符串进行操作。
  36. * boolean b = m.matches();
  37. *
  38. *
  39. */
  40. public static void functionDemo_4() {
  41. String str = "da jia hao,ming tian bu fang jia!";
  42. String regex = "\\b[a-z]{3}\\b";
  43. //1,将正则封装成对象。
  44. Pattern p = Pattern.compile(regex);
  45. //2,通过正则对象获取匹配器对象。
  46. Matcher m = p.matcher(str);
  47. //使用Matcher对象的方法对字符串进行操作。
  48. //既然要获取三个字母组成的单词
  49. //查找。 find();
  50. System.out.println(str);
  51. while(m.find()){
  52. System.out.println(m.group());//获取匹配的子序列
  53. System.out.println(m.start()+":"+m.end());
  54. }
  55. }
  56.  
  57. /*
  58. * 替换
  59. */
  60. public static void functionDemo_3() {
  61. String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu";
  62. str = str.replaceAll("(.)\\1+","$1");
  63. System.out.println(str);
  64. String tel = "15800001111";//158****1111;
  65. tel = tel.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
  66. System.out.println(tel);
  67. }
  68.  
  69. /*
  70. * 切割。
  71. *
  72. * 组:((A)(B(C)))
  73. */
  74. public static void functionDemo_2(){
  75.  
  76. String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu";
  77. String[] names = str.split("(.)\\1+");//str.split("\\.");
  78. for(String name : names){
  79. System.out.println(name);
  80. }
  81. }
  82. /*
  83. * 演示匹配。
  84. */
  85. public static void functionDemo_1(){
  86. //匹配手机号码是否正确。
  87. String tel = "15800001111";
  88. String regex = "1[358]\\d{9}";
  89. boolean b = tel.matches(regex);
  90. System.out.println(tel+":"+b);
  91. }
  92.  
  93. }



加了一对圆括号的为一组,看作为一组。可以通过美元符合$进行引用。
  1. package cn.itcast.regex.test;
  2.  
  3. import java.util.TreeSet;
  4.  
  5. public class RegexTest {
  6.  
  7. /**
  8. * @param args
  9. */
  10. public static void main(String[] args) {
  11.  
  12. /*
  13. * 1,治疗口吃:我我...我我...我我我要...要要要要...要要要要..学学学学学...学学编编...编编编编..编..程程...程程...程程程
  14. * 2,对ip地址排序。
  15. * 3,对邮件地址校验。
  16. */
  17. test_3();
  18. }
  19. //对邮件地址校验。
  20. public static void test_3() {
  21. String mail = "abc1@sina.com.cn";
  22. String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{1,3})+";
  23. regex = "\\w+@\\w+(\\.\\w+)+";//1@1.1
  24. boolean b = mail.matches(regex);
  25. System.out.println(mail+":"+b);
  26. }
  27. /*
  28. * 1,治口吃。
  29. */
  30. public static void test_1(){
  31. String str = "我我...我我...我我我要...要要要要...要要要要..学学学学学...学学编编...编编编编..编..程程...程程...程程程";
  32. //1,将字符串中.去掉。 用替换。
  33. str = str.replaceAll("\\.+","");
  34. System.out.println(str);
  35. //2,替换叠词。
  36. str = str.replaceAll("(.)\\1+","$1");
  37. System.out.println(str);
  38. }
  39. /*
  40. * ip地址排序。
  41. *
  42. * 192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55
  43. */
  44. public static void test_2(){
  45. String ip_str = "192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55";
  46. //1,为了让ip可以按照字符串顺序比较,只要让ip的每一段的位数相同。
  47. //所以,补零,按照每一位所需做多0进行补充。每一段都加两个0.
  48. ip_str = ip_str.replaceAll("(\\d+)","00$1");
  49. System.out.println(ip_str);
  50. //然后每一段保留数字3位。
  51. ip_str = ip_str.replaceAll("0*(\\d{3})","$1");
  52. System.out.println(ip_str);
  53. //1,将ip地址切出。
  54. String[] ips = ip_str.split(" +");
  55. TreeSet<String> ts = new TreeSet<String>();
  56. for(String ip : ips){
  57. // System.out.println(ip);
  58. ts.add(ip);
  59. }
  60. for(String ip : ts){
  61. System.out.println(ip.replaceAll("0*(\\d+)","$1"));
  62. }
  63. }
  64.  
  65. }
  66.  


网页爬虫
  1. package cn.itcast.regex.test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.URL;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13.  
  14. /*
  15. * 网页爬虫:其实就一个程序用于在互联网中获取符合指定规则的数据。
  16. *
  17. * 爬取邮箱地址。
  18. *
  19. */
  20. public class RegexTest2 {
  21.  
  22. /**
  23. * @param args
  24. * @throws IOException
  25. */
  26. public static void main(String[] args) throws IOException {
  27.  
  28. List<String> list = getMailsByWeb();
  29. for(String mail : list){
  30. System.out.println(mail);
  31. }
  32. }
  33. public static List<String> getMailsByWeb() throws IOException {
  34. //1,读取源文件
  35. // BufferedReader bufr = new BufferedReader(new FileReader("c:\\mail.html"));
  36. URL url = new URL("http://192.168.1.100:8080/myweb/mail.html");
  37. BufferedReader bufIn = new BufferedReader(new InputStreamReader(url.openStream()));
  38. //2,对读取的数据进行规则的匹配。从中获取符合规则的数据.
  39. String mail_regex = "\\w+@\\w+(\\.\\w+)+";
  40. List<String> list = new ArrayList<String>();
  41. Pattern p = Pattern.compile(mail_regex);
  42. String line = null;
  43. while((line=bufIn.readLine())!=null){
  44. Matcher m = p.matcher(line);
  45. while(m.find()){
  46. //3,将符合规则的数据存储到集合中。
  47. list.add(m.group());
  48. }
  49. }
  50. return list;
  51. }
  52.  
  53. public static List<String> getMails() throws IOException{
  54. //1,读取源文件
  55. BufferedReader bufr = new BufferedReader(new FileReader("c:\\mail.html"));
  56. //2,对读取的数据进行规则的匹配。从中获取符合规则的数据.
  57. String mail_regex = "\\w+@\\w+(\\.\\w+)+";
  58. List<String> list = new ArrayList<String>();
  59. Pattern p = Pattern.compile(mail_regex);
  60. String line = null;
  61. while((line=bufr.readLine())!=null){
  62. Matcher m = p.matcher(line);
  63. while(m.find()){
  64. //3,将符合规则的数据存储到集合中。
  65. list.add(m.group());
  66. }
  67. }
  68. return list;
  69. }
  70.  
  71. }

猜你在找的正则表达式相关文章