Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

An example of using Java regular expression

Shulou Source: shulou.com Published: 2022-06-02 05:47:22 09月16日 Update

This article mainly explains the "examples of the use of Java regular expressions", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "the use of Java regular expressions" bar!

One: grab the Email address in the web page

Using regular expressions to match text in web pages

The copy code is as follows:

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

Segmentation and extraction of web content

Import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;public class EmailSpider {public static void main (String [] args) {try {BufferedReader br = new BufferedReader (new FileReader ("C:\\ emailSpider.html")); String line= ""; while ((line=br.readLine ())! = null) {parse (line) } catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();}} private static void parse (String line) {Pattern p = Pattern.compile ("[\\ w [. -]] + @ [\\ w [. -]] +\ .[\\ w] +"); Matcher m = p.matcher (line) While (m.find ()) {System.out.println (m.group ());}

Print the results:

867124664@qq.com

260678675@QQ.com

806208721@qq.com

Hr_1985@163.com

32575987@qq.com

Qingchen0501@126.com

Yingyihanxin@foxmail.com

1170382650@qq.com

1170382650@qq.com

Yingyihanxin@foxmail.com

Qingchen0501@126.com

32575987@qq.com

Hr_1985@163.com

Now that you have found so many email addresses, with the knowledge of JavaMail, you can send spam in groups, hehe!

Two: code statistics

Import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class CodeCounter {static long normalLines = 0ram / normal line of code static long commentLines = 0ram / comment line static long whiteLines = 0 / / blank line public static void main (String [] args) {/ / found a folder under which there is no folder, and there is no writing to recursively process files File f = new File that are not in the same folder ("E:\\ Workspaces\\ eclipse\\ Application\\ JavaMailTest\\ src\ com\\ java\\ mail"); File [] codeFiles = f.listFiles () For (File child: codeFiles) {/ / only count java files if (child.getName (). Matches (". *\\ .java $") {parse (child);}} System.out.println ("normalLines:" + normalLines); System.out.println ("commentLines:" + commentLines); System.out.println ("whiteLines:" + whiteLines);} private static void parse (File f) {BufferedReader br = null / / indicates whether to start with a comment boolean comment = false; try {br = new BufferedReader (new FileReader (f)); String line = ""; while ((line = br.readLine ())! = null) {/ / remove the white space that may appear before the comment character / * line = line.trim () / / Blank line because the newline character\ n / / was removed when readLine () fetched the string, so it is not "^ [\\ swords & [^\\ n]] *\ n $" if (line.matches ("^ [\\ slots & [^\\ n]] * $") {whiteLines + + } else if (line.startsWith ("/ *") & &! line.endsWith ("* /") {/ / statistics multiple lines / * / commentLines + +; comment = true;} else if (line.startsWith ("/ *") & & line.endsWith ("* /")) {/ / statistics one line / * / commentLines + + } else if (true = = comment) {/ / Statistics * / commentLines + +; if (line.endsWith ("* /")) {comment = false;}} else if (line.startsWith ("/ /")) {commentLines + +;} else {normalLines + + }} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {if (br! = null) {try {br.close (); br = null;} catch (IOException e) {e.printStackTrace () Thank you for your reading. The above is the content of "examples of the use of Java regular expressions". After the study of this article, I believe you have a deeper understanding of the use of Java regular expressions, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: File regular expression statistics instance code folder content comment web page learning address knowledge blank line garbage character string that is ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn MySQL Linux MariaDB