zoukankan      html  css  js  c++  java
  • 正则表达式

    java.util.regex包主要包括以下三个类:

    • Pattern类:

      pattern对象是一个正则表达式的编译表示。Pattern类没有公共构造方法。要创建一个Pattern对象,你必须首先调用其公共静态编译方法,它返回一个Pattern对象。该方法接受一个正则表达式作为它的第一个参数。

    • Matcher类:

      Matcher对象是对输入字符串进行解释和匹配操作的引擎。与Pattern类一样,Matcher也没有公共构造方法。你需要调用Pattern对象的matcher方法来获得一个Matcher对象。

    • PatternSyntaxException:

      PatternSyntaxException是一个非强制异常类,它表示一个正则表达式模式中的语法错误。

    捕获组

    捕获组是把多个字符当一个单独单元进行处理的方法,它通过对括号内的字符分组来创建。

    例如,正则表达式(dog) 创建了单一分组,组里包含"d","o",和"g"。

    捕获组是通过从左至右计算其开括号来编号。例如,在表达式((A)(B(C))),有四个这样的组:

    • ((A)(B(C)))
    • (A)
    • (B(C))
    • (C)

    可以通过调用matcher对象的groupCount方法来查看表达式有多少个分组。groupCount方法返回一个int值,表示matcher对象当前有多个捕获组。

    还有一个特殊的组(组0),它总是代表整个表达式。该组不包括在groupCount的返回值中。

    下面的例子说明如何从一个给定的字符串中找到数字串:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class RegexMatches
    {
        public static void main( String args[] ){
    
          // 按指定模式在字符串查找
          String line = "This order was placed for QT3000! OK?";
          String pattern = "(.*)(\d+)(.*)";
    
          // 创建 Pattern 对象
          Pattern r = Pattern.compile(pattern);
    
          // 现在创建 matcher 对象
          Matcher m = r.matcher(line);
          if (m.find( )) {
             System.out.println("Found value: " + m.group(0) );
             System.out.println("Found value: " + m.group(1) );
             System.out.println("Found value: " + m.group(2) );
          } else {
             System.out.println("NO MATCH");
          }
       }
    }

    以上实例编译运行结果如下:

    Found value: This order was placed for QT3000! OK?
    Found value: This order was placed for QT300
    Found value: 0

    在替换replaceAll中$可以捕获组

    String str="00123.0565wee.025";
    String str1=str.replaceAll("0*(\d+)(\w+)", "$2");//用第2组中的内容代替前面的0*(\d+)(\w+)
    System.out.println(str+"====="+str1);

    output:

        00123.0565wee.025=====3.wee.5

    叠词切割字符串:

    String reg1="(.)\1+"; //分组,第1组中重复多次
    String str="zvxnvkkkldoiywwkoplm11fgads";
    String[] sp=str.split(reg1);
    for(String s:sp){
    System.out.print(s+" ");
    }

    输出:

    zvxnv ldoiy koplm fgads

    网页爬虫 邮箱号码:

    public class RegTest1 {

    public static void main(String[] args) throws IOException {
    String reg = "\w+@\w+(\.\w+)+";
    URL url= new URL("http://tieba.baidu.com/p/706846476");
    InputStream in = url.openStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    OutputStream bw = new FileOutputStream(new File("f:\mail.txt"), true);
    Pattern p =Pattern.compile(reg);
    String line=null;
    byte[] mail;
    while((line=br.readLine())!=null){
    Matcher m = p.matcher(line);
    if(m.find()){
    mail = (m.group()+" ").getBytes();
    bw.write(mail);
    }
    }

    //下载python软件。

    URL url= new URL("https://www.python.org/ftp/python/2.7/python-2.7.msi");
    String name = url.getFile();
    System.out.println(name);
    InputStream in = url.openStream();
    OutputStream bw = new FileOutputStream(new File("f:\python.msi"));
    int file;
    while((file=in.read())!=-1){
    bw.write(file);
    }
    bw.flush();
    bw.close();
    System.out.println("download successful");

    }

    }

  • 相关阅读:
    [Everyday Mathematics]20150226
    [Everyday Mathematics]20150225
    [Everyday Mathematics]20150224
    [Everyday Mathematics]20150223
    [Everyday Mathematics]20150222
    [Everyday Mathematics]20150221
    [Everyday Mathematics]20150220
    [Everyday Mathematics]20150219
    [Everyday Mathematics]20150218
    [Everyday Mathematic]20150217
  • 原文地址:https://www.cnblogs.com/daxiong225/p/4760493.html
Copyright © 2011-2022 走看看