Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of using Java regular expression

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report