登录  /  注册
首页 > web前端 > js教程 > 正文

使用正则表达式对象实现正则获取步骤详解

php中世界最好的语言
发布: 2018-05-25 10:03:26
原创
1624人浏览过

这次给大家带来使用正则表达式对象实现正则获取步骤详解,使用正则表达式对象实现正则获取的注意事项有哪些,下面就是实战案例,一起来看一下。

获取需要使用到正则的两个对象:

使用的是用正则对象Pattern 和匹配器Matcher。

用法:

范例:

 Pattern p = Pattern.compile("a*b");
 Matcher m = p.matcher("aaaaab");
 boolean b = m.matches();
登录后复制

步骤:

1,先将正则表达式编译成正则对象。使用的是Pattern类一个静态的方法。compile(regex);

2,让正则对象和要操作的字符串相关联,通过matcher方法完成,并返回匹配器对象。

3,通过匹配器对象的方法将正则模式作用到字符串上对字符串进行针对性的功能操作

需求:获取由3个字母组成的单词。

public static void getDemo()
{
  String str = "da jia zhu yi le,ming tian bu fang jia,xie xie!";
  //想要获取由3个字母组成的单词。
  //刚才的功能返回的都是一个结果,只有split返回的是数组,但是它是把规则作为分隔符,不会获取符合规则的内容。
  //这时我们要用到一些正则对象。
  String reg = "\\b[a-z]{3}\\b";
  Pattern p = Pattern.compile(reg);
  Matcher m = p.matcher(str);
  while(m.find())
  {
    System.out.println(m.start()+"...."+m.end());
    System.out.println("sub:"+str.substring(m.start(),m.end()));
    System.out.println(m.group());
  }
//   System.out.println(m.find());//将规则对字符串进行匹配查找。
//   System.out.println(m.find());//将规则对字符串进行匹配查找。
//   System.out.println(m.group());//在使用group方法之前,必须要先找,找到了才可以取。
}
登录后复制

校验邮件

public static void checkMail()
{
  String mail = "abc123@sina.com.cn";
  mail = "1@1.1";
  String reg = "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
  reg = "\\w+@\\w+(\\.\\w+)+";//简化的规则。笼统的匹配。
  boolean b = mail.matches(reg);
  System.out.println(mail+":"+b);
}
登录后复制

网络爬虫 (获取邮箱)

class GetMailList 
{
public static void main(String[] args) throws Exception
{
  String reg = "\\w+@[a-zA-Z]+(\\.[a-zA-Z]+)+";
  getMailsByWeb(reg);
}
public static void getMailsByWeb(String regex)throws Exception
{
  URL url = new URL("http://localhost:8080/myweb/mail.html");
  URLConnection conn = url.openConnection();
  BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  String line = null;
  Pattern p = Pattern.compile(regex);   
  while((line=bufIn.readLine())!=null)
  {
    //System.out.println(line);
    Matcher m = p.matcher(line);
    while(m.find())
    {
      System.out.println(m.group());
    }
  }
  bufIn.close();
}
public static void getMails(String regex)throws Exception
{
  BufferedReader bufr = 
    new BufferedReader(new FileReader("mail.txt"));
  String line = null;
  Pattern p = Pattern.compile(regex);   
  while((line=bufr.readLine())!=null)
  {
    //System.out.println(line);
    Matcher m = p.matcher(line);
    while(m.find())
    {
      System.out.println(m.group());
    }
  }
  bufr.close();
}
}
登录后复制

单词边界匹配器 \b

\b代表一个单词的开始和结束部分,不匹配任何字符

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

使用前端方法实现图片转字符画

JS数组方法使用步骤详解

以上就是使用正则表达式对象实现正则获取步骤详解的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号