• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

java正则表达式简单应用

java 搞代码 4年前 (2022-01-05) 38次浏览 已收录 0个评论

这篇文章主要介绍了java正则表达式简单应用,在之前几篇文章中已经深入学习了java正则表达式基础知识,本文对java正则表达式应用进行研究,感兴趣的小伙伴们可以参考一下

一:抓取网页中的Email地址

利用正则表达式匹配网页中的文本

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

将网页内容分割提取

 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()); } } } 

打印结果:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
现在你找到这么多邮箱地址,用上JavaMail的知识,你可以群发垃圾邮件了,呵呵!!!

二:代码统计

 import java.io.BufferedReade<strong style="color:transparent">来源gao@daima#com搞(%代@#码@网</strong>r; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class CodeCounter { static long normalLines = 0;//正常代码行 static long commentLines = 0;//注释行 static long whiteLines = 0;//空白行 public static void main(String[] args) { //找到某个文件夹,该文件夹下面在没有文件夹,这里没有写递归处理不在同一文件夹的文件 File f = new File("E:\\Workspaces\\eclipse\\Application\\JavaMailTest\\src\\com\\java\\mail"); File[] codeFiles = f.listFiles(); for(File child : codeFiles){ //只统计java文件 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; //表示是否为注释开始 boolean comment = false; try { br = new BufferedReader(new FileReader(f)); String line = ""; while((line = br.readLine()) != null) { //去掉注释符/*前面可能出现的空白 line = line.trim(); //空行 因为readLine()将字符串取出来时,已经去掉了换行符\n //所以不是"^[\\s&&[^\\n]]*\\n$" if(line.matches("^[\\s&&[^\\n]]*$")) { whiteLines ++; } else if (line.startsWith("/*") && !line.endsWith("*/")) { //统计多行/*****/ commentLines ++; comment = true; } else if (line.startsWith("/*") && line.endsWith("*/")) { //统计一行/**/ commentLines ++; } else if (true == comment) { //统计*/ 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(); } } } } } 

以上就是针对java正则表达式的简单应用,希望对大家的学习Java正则表达式有所帮助。

以上就是java正则表达式简单应用的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java正则表达式简单应用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址