Java常用工具类三 -- 正则表达式
来源:互联网 发布:sql insert case when 编辑:程序博客网 时间:2024/05/21 09:04
1.抓取网页email地址实例
实现思路:
1、使用java.net.URL对象,绑定网络上某一个网页的地址
2、通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象
3、通过HttpConnection对象的getInputStream()方法获得该网络文件的输入流对象InputStream
4、循环读取流中的每一行数据,并由Pattern对象编译的正则表达式区配每一行字符,取得email地址
- package regex;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * 网络爬虫,抓取网页中的email地址
- */
- public class WebCrawlersDemo {
- public static void main(String[] args) throws Exception {
- URL url = new URL("http://www.tianya.cn/publicforum/content/english/1/129176.shtml");
- // 打开连接
- URLConnection conn = url.openConnection();
- // 设置连接网络超时时间
- conn.setConnectTimeout(1000 * 10);
- // 读取指定网络地址中的文件
- BufferedReader bufr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line = null;
- String regex = "[a-zA-Z0-9_-]+@\\w+\\.[a-z]+(\\.[a-z]+)?"; // 匹配email的正则
- Pattern p = Pattern.compile(regex);
- while((line = bufr.readLine()) != null) {
- Matcher m = p.matcher(line);
- while(m.find()) {
- System.out.println(m.group());<span style="white-space:pre"> </span>// 获得匹配的email
- }
- }
- }
- }
2.常用正则工具类 --验证手机,身份证,邮箱等
此类提供日常开发中常用的正则验证函数,比如:邮箱、手机号、电话号码、身份证号码、日期、数字、小数、URL、IP地址等。使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于:
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
每个正则可能还有待优化的地方,您如有更好的方式实现某一个功能的验证,欢迎提出来大家一起讨论。下面是工具类的完整代码:
- /**
- * 正则工具类
- * 提供验证邮箱、手机号、电话号码、身份证号码、数字等方法
- */
- public final class RegexUtils {
- /**
- * 验证Email
- * @param email email地址,格式:zhangsan@sina.com,zhangsan@xxx.com.cn,xxx代表邮件服务商
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkEmail(String email) {
- String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
- return Pattern.matches(regex, email);
- }
- /**
- * 验证身份证号码
- * @param idCard 居民身份证号码15位或18位,最后一位可能是数字或字母
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkIdCard(String idCard) {
- String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
- return Pattern.matches(regex,idCard);
- }
- /**
- * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))
- * @param mobile 移动、联通、电信运营商的号码段
- *<p>移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡)
- *、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)</p>
- *<p>联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)</p>
- *<p>电信的号段:133、153、180(未启用)、189</p>
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkMobile(String mobile) {
- String regex = "(\\+\\d+)?1[3458]\\d{9}$";
- return Pattern.matches(regex,mobile);
- }
- /**
- * 验证固定电话号码
- * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447
- * <p><b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字,
- * 数字之后是空格分隔的国家(地区)代码。</p>
- * <p><b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号——
- * 对不使用地区或城市代码的国家(地区),则省略该组件。</p>
- * <p><b>电话号码:</b>这包含从 0 到 9 的一个或多个数字 </p>
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkPhone(String phone) {
- String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
- return Pattern.matches(regex, phone);
- }
- /**
- * 验证整数(正整数和负整数)
- * @param digit 一位或多位0-9之间的整数
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkDigit(String digit) {
- String regex = "\\-?[1-9]\\d+";
- return Pattern.matches(regex,digit);
- }
- /**
- * 验证整数和浮点数(正负整数和正负浮点数)
- * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkDecimals(String decimals) {
- String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
- return Pattern.matches(regex,decimals);
- }
- /**
- * 验证空白字符
- * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkBlankSpace(String blankSpace) {
- String regex = "\\s+";
- return Pattern.matches(regex,blankSpace);
- }
- /**
- * 验证中文
- * @param chinese 中文字符
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkChinese(String chinese) {
- String regex = "^[\u4E00-\u9FA5]+$";
- return Pattern.matches(regex,chinese);
- }
- /**
- * 验证日期(年月日)
- * @param birthday 日期,格式:1992-09-03,或1992.09.03
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkBirthday(String birthday) {
- String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
- return Pattern.matches(regex,birthday);
- }
- /**
- * 验证URL地址
- * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkURL(String url) {
- String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";
- return Pattern.matches(regex, url);
- }
- /**
- * 匹配中国邮政编码
- * @param postcode 邮政编码
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkPostcode(String postcode) {
- String regex = "[1-9]\\d{5}";
- return Pattern.matches(regex, postcode);
- }
- /**
- * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小)
- * @param ipAddress IPv4标准地址
- * @return 验证成功返回true,验证失败返回false
- */
- public static boolean checkIpAddress(String ipAddress) {
- String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";
- return Pattern.matches(regex, ipAddress);
- }
- }
单元测试类完整代码:
- /**
- * 正则表达式工具类测试
- */
- public class RegexUtilsTest {
- /**
- * 验证邮箱
- */
- @Test
- public void testCheckEmail() {
- boolean result = RegexUtils.checkEmail("zha2_ngsan@sina.com.cn");
- Assert.assertTrue(result);
- }
- /**
- * 验证身份证号码
- */
- @Test
- public void testCheckIdCard() {
- boolean result = RegexUtils.checkIdCard("432403193902273273");
- Assert.assertTrue(result);
- }
- /**
- * 验证手机号码
- */
- @Test
- public void testCheckMobile() {
- boolean result = RegexUtils.checkMobile("+8613620285733");
- Assert.assertTrue(result);
- }
- /**
- * 验证电话号码
- */
- @Test
- public void testCheckPhone() {
- boolean result = RegexUtils.checkPhone("+860738-4630706");
- Assert.assertTrue(result);
- }
- /**
- * 验证整数(正整数和负整数)
- */
- @Test
- public void testCheckDigit() {
- boolean result = RegexUtils.checkDigit("123132");
- Assert.assertTrue(result);
- }
- /**
- * 验证小数和整数(正负整数和正负小数)
- */
- @Test
- public void testCheckDecimals() {
- boolean result = RegexUtils.checkDecimals("-33.2");
- Assert.assertTrue(result);
- }
- /**
- * 验证空白字符
- */
- @Test
- public void testCheckBlankSpace() {
- boolean result = RegexUtils.checkBlankSpace(" ");
- Assert.assertTrue(result);
- }
- /**
- * 匹配中文
- */
- @Test
- public void testCheckChinese() {
- boolean result = RegexUtils.checkChinese("中文");
- Assert.assertTrue(result);
- }
- /**
- * 验证日期
- */
- @Test
- public void testCheckBirthday() {
- boolean result = RegexUtils.checkBirthday("1992/09/03");
- Assert.assertTrue(result);
- }
- /**
- * 验证中国邮政编码
- */
- @Test
- public void testCheckPostcode() {
- boolean result = RegexUtils.checkPostcode("417100");
- Assert.assertTrue(result);
- }
- /**
- * 验证URL地址
- */
- @Test
- public void testCheckURL() {
- boolean result = RegexUtils.checkURL("http://blog.csdn.com:80/xyang81/article/details?name=&abc=中文");
- Assert.assertTrue(result);
- }
- /**
- * 验证IP地址
- */
- @Test
- public void testCheckIpAddress() {
- boolean result = RegexUtils.checkIpAddress("192.1.22.255");
- Assert.assertTrue(result);
- }
- }
测试结果:
3.代码量统计工具(统计java源文件中注释、代码、空白行数量)
比如想统计一个Java程序员一天写代码的工作量(如:有效代码多少行、空行多少、注释多少行等),这个小工具也许能做为一个参考的依据。
思路:因为每个java源文件的内容基本包括java语句、空白行、注释三部份组成(不包括注解),所以要统计某个文件这三部份的内容各占多少时,只需写三个匹配这几部份内容的正则表达式即可。然后通过IO流读取文件中的每一行,并根据正则匹配的结果,累加每部份匹配的数量即可。
注释行:单行注释(//)、多行注释、文档注释。正则:((//)|(/\\*+)|((^\\s)*\\*)|((^\\s)*\\*+/))+
空白行:一行内只有空格、\t、\n等非可视字符表示空白行。正则:^\\s*$
代码行:以分号“;“结束为一行有效的代码。正则:(?!import|package).+;\\s*(((//)|(/\\*+)).*)*
实现方式:
1、用户在控制台输入要统计的java文件路径或目录
2、根据路径创建一个File对象,判断文件是否有效,并给出提示。最后将文件和IO流绑定
3、循环读取文件中的某一行,并根据正则匹配的结果累记每部份匹配的数量
- package regex;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileFilter;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- import java.util.regex.Pattern;
- /**
- * 代码统计工具
- * 统计某个java源文件或某个目录中所有java源文件中注释、空白行、代码行各占多少行
- * 注释:包括单行注释(//)、多行注释、文档注释
- * 空白行:一行内没有任何内容的表示空白行
- * 代码行:以分号“;”结束的一条语句,可以统计为一行有效代码
- */
- public class CodeStatDemo {
- // 记录注释行数
- static long annotationLine = 0;
- // 记录空白行数
- static long blankLine = 0;
- // 记录有效代码的行数
- static long codeLine = 0;
- // 代码总行数
- static long totalLine = 0;
- // 文件总数
- static long fileCount = 0;
- public static void main(String[] args) throws FileNotFoundException {
- System.out.println("请输入要统计代码量的java文件或java目录:");
- Scanner in = new Scanner(System.in);
- String filePath = in.nextLine();
- File file = new File(filePath);
- // 根据用户输入的文件名和目录执行代码量统计
- codeStat(file);
- System.out.println("----------统计结果---------");
- System.out.println("文件数量:" + fileCount + "个");
- System.out.println(file + "文件/目录总行数:" + totalLine);
- System.out.println("代码行数:" + codeLine);
- System.out.println("注释行数:" + annotationLine);
- System.out.println("空白行数:" + blankLine);
- long otherLine = totalLine - (codeLine + annotationLine + blankLine);
- System.out.println("其它行数:" + otherLine);
- }
- private static void codeStat(File file) throws FileNotFoundException {
- if (file == null || !file.exists())
- throw new FileNotFoundException(file + ",文件不存在!");
- fileCount ++; // 文件数累加
- if (file.isDirectory()) {
- File[] files = file.listFiles(new FileFilter() {
- @Override
- public boolean accept(File pathname) {
- return pathname.getName().endsWith(".java") || pathname.isDirectory();
- }
- });
- for (File target : files) {
- codeStat(target);
- }
- } else {
- BufferedReader bufr = null;
- try {
- // 将指定路径的文件与字符流绑定
- bufr = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
- } catch (FileNotFoundException e) {
- throw new FileNotFoundException(file + ",文件不存在!" + e);
- }
- // 定义匹配每一行的正则匹配器
- Pattern annotationLinePattern = Pattern.compile("((//)|(/\\*+)|((^\\s)*\\*)|((^\\s)*\\*+/))+",
- Pattern.MULTILINE + Pattern.DOTALL); // 注释匹配器(匹配单行、多行、文档注释)
- Pattern blankLinePattern = Pattern.compile("^\\s*$"); // 空白行匹配器(匹配回车、tab键、空格)
- Pattern codeLinePattern = Pattern.compile("(?!import|package).+;\\s*(((//)|(/\\*+)).*)*",
- Pattern.MULTILINE + Pattern.DOTALL); // 代码行匹配器(以分号结束为一行有效语句,但不包括import和package语句)
- // 遍历文件中的每一行,并根据正则匹配的结果记录每一行匹配的结果
- String line = null;
- try {
- while((line = bufr.readLine()) != null) {
- if (annotationLinePattern.matcher(line).find()) {
- annotationLine ++;
- }
- if (blankLinePattern.matcher(line).find()) {
- blankLine ++;
- }
- if (codeLinePattern.matcher(line).matches()) {
- codeLine ++;
- }
- totalLine ++;
- }
- } catch (IOException e) {
- throw new RuntimeException("读取文件失败!" + e);
- } finally {
- try {
- bufr.close(); // 关闭文件输入流并释放系统资源
- } catch (IOException e) {
- throw new RuntimeException("关闭文件输入流失败!");
- }
- }
- }
- }
- }
- Java常用工具类三 -- 正则表达式
- JS正则表达式常用工具
- java正则表达式(三)
- java常用工具类(三)
- Java常用工具类总结(三)
- JAVA正则表达式(三)
- java正则表达式(三)
- Java常用工具类(三)常用类
- Java常用工具类集合(三)
- 正则表达式工具类,正则表达式封装,Java正则表达式
- Java字符串(三)--正则表达式
- Java--正则表达式基础入门(三)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(三)
- 共同学习Java源代码--常用工具类--StringBuffer(三)
- Java正则表达式常用正则工具类
- Java正则表达式、常用正则工具类
- Java 正则表达式正则
- 【正则表达式】Java正则表达式
- 淘宝商城model部分
- bbed初体验
- android 水平button等宽
- git 的本地基本操作
- C#中将结构类型数据存储到二进制文件中方法
- Java常用工具类三 -- 正则表达式
- alsa 音频库的移植
- 查了查jtag和串口的区别
- android开发TextView中一些属性的设置
- Binary Tree Postorder Traversal
- linux里的命令
- Clone Graph
- Java异常机制,你知道吗?
- 淘宝商城servlet部分