SoFunction
Updated on 2025-03-09

Java regular expression usage

1: Crawl the email address in the web page

Use regular expressions to match text in web pages

Copy the codeThe code is as follows:

[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+

Split and extract web page content

import ;
import ;
import ;
import ;
import ;
import ;
public class EmailSpider {
  public static void main(String[] args) {
    try {
      BufferedReader br = new BufferedReader(new FileReader("C:\\"));
      String line = "";
      while((line=()) != null) {
        parse(line);
      }
    } catch (FileNotFoundException e) {
      ();
    } catch (IOException e) {
      ();
    }
  }
  private static void parse(String line) {
    Pattern p = ("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
    Matcher m = (line);
    while(()) {
      (());
    }
  }
}

Print result:

867124664@
260678675@
806208721@
hr_1985@
32575987@
qingchen0501@
yingyihanxin@
1170382650@
1170382650@
yingyihanxin@
qingchen0501@
32575987@
hr_1985@

Now that you have found so many email addresses, using JavaMail knowledge, you can send mass spam, haha! ! !

Two: Code statistics

import ;
import ;
import ;
import ;
import ;
public class CodeCounter {
  static long normalLines = 0;//Normal code line  static long commentLines = 0;//Comment line  static long whiteLines = 0;//Blank line  public static void main(String[] args) {
    // Found a folder, there is no folder under the folder, and there is no recursive processing of files not in the same folder.    File f = new File("E:\\Workspaces\\eclipse\\Application\\JavaMailTest\\src\\com\\java\\mail");
    File[] codeFiles = ();
    for(File child : codeFiles){
      //Only count java files      if(().matches(".*\\.java$")) {
        parse(child);
      }
    }
    ("normalLines:" + normalLines);
    ("commentLines:" + commentLines);
    ("whiteLines:" + whiteLines);
  }
  private static void parse(File f) {
    BufferedReader br = null;
    //Indicate whether it is the beginning of the comment    boolean comment = false;
    try {
      br = new BufferedReader(new FileReader(f));
      String line = "";
      while((line = ()) != null) {
        //Remove the comments/*The possible blank spaces in front         line = ();
         //Blank lines Because when readLine() takes out the string, the newline character has been removed\n
         //So it is not "^[\\s&&[^\\n]]*\\n$"
         if(("^[\\s&&[^\\n]]*$")) {
           whiteLines ++;
         } else if (("/*") && !("*/")) {
          //Statistics multiple lines/******/          commentLines ++;
          comment = true;  
        } else if (("/*") && ("*/")) {
          //Statistics line/**/           commentLines ++;
         } else if (true == comment) {
           //statistics*/
          commentLines ++;
          if(("*/")) {
            comment = false;
          }
        } else if (("//")) {
          commentLines ++;
        } else {
          normalLines ++;
        }
      }
    } catch (FileNotFoundException e) {
      ();
    } catch (IOException e) {
      ();
    } finally {
      if(br != null) {
        try {
          ();
          br = null;
        } catch (IOException e) {
          ();
        }
      }
    }
  }
}

The above content is the use of Java in regular expressions shared by this article. I hope you like it.