`
lzkyo
  • 浏览: 463881 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java Regex To Use

    博客分类:
  • Java
阅读更多
Java代码 复制代码
  1.  /**  
  2.   * 得到文件所在的磁盘目录  
  3.   * @param file  
  4.   * @return  
  5.   */  
  6.  public static String getFileDirectory(String file){   
  7.   String regEx = "[a-zA-z]{1,4}:.*[\\\\/]";   
  8.   String dir = "";   
  9.         Pattern p=Pattern.compile(regEx);    
  10.   Matcher m=p.matcher(file);    
  11.   if(m.find()){   
  12.    dir = m.group(m.groupCount());   
  13.   }   
  14.   return  dir;   
  15.  }   
  16.     
  17.  /**  
  18.   * 得到文件名  
  19.   * @param file  
  20.   * @return  
  21.   */  
  22.  public static String getFileName(String file){   
  23.   String regEx =".+[\\\\|/](.+)$";   
  24.   String fileName = "";   
  25.         Pattern p=Pattern.compile(regEx);    
  26.   Matcher m=p.matcher(file);    
  27.   if(m.find()){   
  28.    fileName = m.group(m.groupCount());   
  29.   }   
  30.   return  fileName;   
  31.  }   
  32.     
  33.  /**  
  34.   * 得到文件扩展名  
  35.   * @param file  
  36.   * @return  
  37.   */  
  38.  public static String getFileExtName(String file){   
  39.   String regEx = ".*\\.";   
  40.   Pattern p = Pattern.compile(regEx);   
  41.   Matcher m = p.matcher(file);   
  42.   String extName = m.replaceAll("");   
  43.   return extName;   
  44.  }   
  45.   
  46. /**  
  47.  * 当在模式中存在用括号括起来的组时,可以分别检索每个组的匹配值。从最左边的组开始编为1,  
  48.  * 然后依次对每对括号相对应的组进行编号。在下面的模式中,第一组是协议(如http),第二组是域名。  
  49.  * 为了在匹配的字符串中访问组,可以使用Matcher的group方法。  
  50.  */  
  51. public static void getMatchGroup(String data) {   
  52.     String urlString = "(http|https|ftp)://([a-zA-Z0-9-\\.]+)[/\\w\\.\\-\\+\\?%=&;:,#]*";   
  53.     Pattern urlPattern = Pattern.compile(urlString);   
  54.     Matcher urlMatcher = urlPattern.matcher(data);   
  55.     while (urlMatcher.find()) {   
  56.         String domain = urlMatcher.group(2); // 2nd group is the domain   
  57.         System.out.println(domain);   
  58.     }   
  59. }   
  60.   
  61. /**  
  62.  * 在一个模式内引用一个以前的匹配组称为逆向引用(backreference),简化模式书写。  
  63.  * 为了对第三个组进行逆向引用,在模式中包括\3即可。这将会只匹配一个与以前的组相匹配的严格重复的数据。  
  64.  * eg.String data = " The the water molecules are made of of hydrogen and oxygen";  
  65.  */  
  66. public static void getBackReferencesGroup(String data) {   
  67.     //该模式匹配情况如下:一个空白字符、特殊的单词列表中的一个单词、更多的空白、   
  68.     //再次重复的相同的单词(使用\1对第一个组进行逆向引用)以及空白符或标点符号。   
  69.     String patternStr = "\\s(of|or|the|to)\\s+\\1[\\s\\.,;]";   
  70.   
  71.     Pattern wordPattern = Pattern.compile(patternStr,   
  72.     Pattern.CASE_INSENSITIVE);//不区分大小写   
  73.     Matcher wordMatcher = wordPattern.matcher(data);   
  74.     while (wordMatcher.find()) {   
  75.         int start = wordMatcher.start();   
  76.         String word = wordMatcher.group(1);   
  77.         System.out.println("Repeated " + word + " starting at " + start);   
  78.     }   
  79. }   
  80.   
  81. /**  
  82.  * 取大括号内的内容  
  83.  * @param inputStr  
  84.  * @return  
  85.  */  
  86. public static String getBraceContent(String inputStr){   
  87.     Pattern pattern = Pattern.compile("(?<=\\{)[^\\{\\}]*(?=\\})"2);   
  88.     Matcher matcher = pattern.matcher(inputStr);   
  89.     StringBuffer sb = new StringBuffer();   
  90.        
  91.     String temp;   
  92.     while(matcher.find()){   
  93.         temp = inputStr.substring(matcher.start(), matcher.end());   
  94.         sb.append(temp+"\n");   
  95.     }   
  96.        
  97.     return sb.toString();   
  98. }   
  99.   
  100.  /**  
  101.   * 得到html标签的属性  
  102.   * @param html 文件内容  
  103.   * @param label 要提取属性的标签名称,如:font ,img...  
  104.   */  
  105.  public static void getHtmlAttribute(String html,String label){   
  106.   Map mapAttrib = new HashMap();   
  107.   String regEx = "<"+label+"\\s*([^>]*)\\s*>";   
  108.   String regEx2 = "([a-z]+)\\s*=\\s*\"([^\"]+)\"";   
  109.   
  110.   Pattern p = Pattern.compile(regEx);   
  111.   Matcher m = p.matcher(html);   
  112.   if(m.find()){   
  113.    String attribs = m.group(1);   
  114.    p = Pattern.compile(regEx2);   
  115.    m = p.matcher(attribs);   
  116.    while(m.find()){   
  117.     mapAttrib.put(m.group(1), m.group(2));   
  118.    }   
  119.   }   
  120.   printMapData(mapAttrib);   
  121.  }   
  122.     
  123.  public static void printMapData(Map map){   
  124.   Set     entries   =   map.entrySet();    
  125.   Iterator   iter   =   entries.iterator();    
  126.   while(iter.hasNext())    
  127.   {    
  128.          Map.Entry   entry   =   (Map.Entry)iter.next();    
  129.        System.out.println(entry.getKey()+"="+entry.getValue());   
  130.   }    
  131.  }   
  132.   
  133.   
  134.  /**  
  135.   * 使用Jacob工具包完成word到html的转换  
  136.   * @param absPath 文件绝对路径  
  137.   */  
  138.  public static boolean wordFormatToHtml(String absPath) throws ProgramException{   
  139.   
  140.      String FileFormat = "";   
  141.      FileFormat = getFileExtName(absPath);//文件类型   
  142.   
  143.      if(FileFormat.equalsIgnoreCase("doc"))   
  144.      {   
  145.          String DocFile = absPath;   
  146.          //word文件的完整路径   
  147.   
  148.          String HtmlFile = DocFile.substring(0, (DocFile.length() - 4)) + ".htm";   
  149.          //html文件的完整路径   
  150.   
  151.          ActiveXComponent app = new ActiveXComponent("Word.Application");   
  152.          //启动word   
  153.   
  154.          try{   
  155.            app.setProperty("Visible"new Variant(false));   
  156.            //设置word程序非可视化运行   
  157.            Dispatch docs = app.getProperty("Documents").toDispatch();   
  158.            Dispatch doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{DocFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();    
  159.            //打开word文件   
  160.            Dispatch oWordBasic = (Dispatch) Dispatch.call(app, "WordBasic").getDispatch();   
  161.               
  162.            Dispatch.call(oWordBasic, "AcceptAllChangesInDoc");   
  163.               
  164.            Dispatch.invoke(doc,"SaveAs",Dispatch.Method, new Object[]{HtmlFile,new Variant(8)}, new int[1]);   
  165.            //作为htm格式保存文件   
  166.   
  167.            Dispatch.call(doc, "Close",new Variant(false));   
  168.            //关闭文件   
  169.          }   
  170.          catch (Exception e)   
  171.          {   
  172.     throw new ProgramException("error$Word转换为HTML时出错!");   
  173.          }   
  174.          finally  
  175.          {   
  176.            app.invoke("Quit"new Variant[] {});   
  177.            //退出word程序   
  178.          }   
  179.          //转化完毕   
  180.          return true;   
  181.      }   
  182.      return false;   
  183.    }   
  184.     
  185.  /**  
  186.   * 逐行读取HTML文件内容  
  187.   * @param filePath  HTML文件的路径  
  188.   * @return  
  189.   * @throws ProgramException  
  190.   */  
  191.  public static String getHTMLContent(String filePath) throws ProgramException{   
  192.   StringBuffer sb=new StringBuffer();   
  193.   try{   
  194.   String line="";   
  195.   File file=new File(filePath);   
  196.   InputStreamReader read = new InputStreamReader (new FileInputStream(file));   
  197.   BufferedReader br=new BufferedReader(read);   
  198.   while((line=br.readLine())!=null){   
  199.    sb.append(line);   
  200.    sb.append('\n');//注意换行符写入   
  201.   }   
  202.   }catch(FileNotFoundException e){   
  203.    throw new ProgramException("error$读HTML文件时,文件没有找到");   
  204.   }catch(IOException e){   
  205.    throw new ProgramException("error$读HTML文件时,出现IO异常");   
  206.   }   
  207.   String temp=sb.toString();   
  208.   //不管图片   
  209.   String regEx = "<img\\s*([^>]*)\\s*>";   
  210.   Pattern p = Pattern.compile(regEx);   
  211.   Matcher m = p.matcher(temp);   
  212.   temp=m.replaceAll("");   
  213.   
  214.   String regEx2 = "<v:imagedata\\s*([^>]*)\\s*>";   
  215.   Pattern p2 = Pattern.compile(regEx2);   
  216.   Matcher m2 = p2.matcher(temp);   
  217.   temp=m2.replaceAll("");   
  218.      
  219.   temp = temp.replace("\'""\"");   
  220.   return temp;   
  221.  }  
 /**
  * 得到文件所在的磁盘目录
  * @param file
  * @return
  */
 public static String getFileDirectory(String file){
  String regEx = "[a-zA-z]{1,4}:.*[\\\\/]";
  String dir = "";
        Pattern p=Pattern.compile(regEx); 
  Matcher m=p.matcher(file); 
  if(m.find()){
   dir = m.group(m.groupCount());
  }
  return  dir;
 }
 
 /**
  * 得到文件名
  * @param file
  * @return
  */
 public static String getFileName(String file){
  String regEx =".+[\\\\|/](.+)$";
  String fileName = "";
        Pattern p=Pattern.compile(regEx); 
  Matcher m=p.matcher(file); 
  if(m.find()){
   fileName = m.group(m.groupCount());
  }
  return  fileName;
 }
 
 /**
  * 得到文件扩展名
  * @param file
  * @return
  */
 public static String getFileExtName(String file){
  String regEx = ".*\\.";
  Pattern p = Pattern.compile(regEx);
  Matcher m = p.matcher(file);
  String extName = m.replaceAll("");
  return extName;
 }

/**
 * 当在模式中存在用括号括起来的组时,可以分别检索每个组的匹配值。从最左边的组开始编为1,
 * 然后依次对每对括号相对应的组进行编号。在下面的模式中,第一组是协议(如http),第二组是域名。
 * 为了在匹配的字符串中访问组,可以使用Matcher的group方法。
 */
public static void getMatchGroup(String data) {
	String urlString = "(http|https|ftp)://([a-zA-Z0-9-\\.]+)[/\\w\\.\\-\\+\\?%=&;:,#]*";
	Pattern urlPattern = Pattern.compile(urlString);
	Matcher urlMatcher = urlPattern.matcher(data);
	while (urlMatcher.find()) {
		String domain = urlMatcher.group(2); // 2nd group is the domain
		System.out.println(domain);
	}
}

/**
 * 在一个模式内引用一个以前的匹配组称为逆向引用(backreference),简化模式书写。
 * 为了对第三个组进行逆向引用,在模式中包括\3即可。这将会只匹配一个与以前的组相匹配的严格重复的数据。
 * eg.String data = " The the water molecules are made of of hydrogen and oxygen";
 */
public static void getBackReferencesGroup(String data) {
	//该模式匹配情况如下:一个空白字符、特殊的单词列表中的一个单词、更多的空白、
	//再次重复的相同的单词(使用\1对第一个组进行逆向引用)以及空白符或标点符号。
	String patternStr = "\\s(of|or|the|to)\\s+\\1[\\s\\.,;]";

	Pattern wordPattern = Pattern.compile(patternStr,
	Pattern.CASE_INSENSITIVE);//不区分大小写
	Matcher wordMatcher = wordPattern.matcher(data);
	while (wordMatcher.find()) {
		int start = wordMatcher.start();
		String word = wordMatcher.group(1);
		System.out.println("Repeated " + word + " starting at " + start);
	}
}

/**
 * 取大括号内的内容
 * @param inputStr
 * @return
 */
public static String getBraceContent(String inputStr){
    Pattern pattern = Pattern.compile("(?<=\\{)[^\\{\\}]*(?=\\})", 2);
    Matcher matcher = pattern.matcher(inputStr);
	StringBuffer sb = new StringBuffer();
	
	String temp;
	while(matcher.find()){
		temp = inputStr.substring(matcher.start(), matcher.end());
		sb.append(temp+"\n");
	}
	
	return sb.toString();
}

 /**
  * 得到html标签的属性
  * @param html 文件内容
  * @param label 要提取属性的标签名称,如:font ,img...
  */
 public static void getHtmlAttribute(String html,String label){
  Map mapAttrib = new HashMap();
  String regEx = "<"+label+"\\s*([^>]*)\\s*>";
  String regEx2 = "([a-z]+)\\s*=\\s*\"([^\"]+)\"";

  Pattern p = Pattern.compile(regEx);
  Matcher m = p.matcher(html);
  if(m.find()){
   String attribs = m.group(1);
   p = Pattern.compile(regEx2);
   m = p.matcher(attribs);
   while(m.find()){
    mapAttrib.put(m.group(1), m.group(2));
   }
  }
  printMapData(mapAttrib);
 }
 
 public static void printMapData(Map map){
  Set     entries   =   map.entrySet(); 
  Iterator   iter   =   entries.iterator(); 
  while(iter.hasNext()) 
  { 
         Map.Entry   entry   =   (Map.Entry)iter.next(); 
       System.out.println(entry.getKey()+"="+entry.getValue());
  } 
 }


 /**
  * 使用Jacob工具包完成word到html的转换
  * @param absPath 文件绝对路径
  */
 public static boolean wordFormatToHtml(String absPath) throws ProgramException{

     String FileFormat = "";
     FileFormat = getFileExtName(absPath);//文件类型

     if(FileFormat.equalsIgnoreCase("doc"))
     {
         String DocFile = absPath;
         //word文件的完整路径

         String HtmlFile = DocFile.substring(0, (DocFile.length() - 4)) + ".htm";
         //html文件的完整路径

         ActiveXComponent app = new ActiveXComponent("Word.Application");
         //启动word

         try{
           app.setProperty("Visible", new Variant(false));
           //设置word程序非可视化运行
           Dispatch docs = app.getProperty("Documents").toDispatch();
           Dispatch doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{DocFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch(); 
           //打开word文件
           Dispatch oWordBasic = (Dispatch) Dispatch.call(app, "WordBasic").getDispatch();
           
           Dispatch.call(oWordBasic, "AcceptAllChangesInDoc");
           
           Dispatch.invoke(doc,"SaveAs",Dispatch.Method, new Object[]{HtmlFile,new Variant(8)}, new int[1]);
           //作为htm格式保存文件

           Dispatch.call(doc, "Close",new Variant(false));
           //关闭文件
         }
         catch (Exception e)
         {
    throw new ProgramException("error$Word转换为HTML时出错!");
         }
         finally
         {
           app.invoke("Quit", new Variant[] {});
           //退出word程序
         }
         //转化完毕
         return true;
     }
     return false;
   }
 
 /**
  * 逐行读取HTML文件内容
  * @param filePath  HTML文件的路径
  * @return
  * @throws ProgramException
  */
 public static String getHTMLContent(String filePath) throws ProgramException{
  StringBuffer sb=new StringBuffer();
  try{
  String line="";
  File file=new File(filePath);
  InputStreamReader read = new InputStreamReader (new FileInputStream(file));
  BufferedReader br=new BufferedReader(read);
  while((line=br.readLine())!=null){
   sb.append(line);
   sb.append('\n');//注意换行符写入
  }
  }catch(FileNotFoundException e){
   throw new ProgramException("error$读HTML文件时,文件没有找到");
  }catch(IOException e){
   throw new ProgramException("error$读HTML文件时,出现IO异常");
  }
  String temp=sb.toString();
  //不管图片
  String regEx = "<img\\s*([^>]*)\\s*>";
  Pattern p = Pattern.compile(regEx);
  Matcher m = p.matcher(temp);
  temp=m.replaceAll("");

  String regEx2 = "<v:imagedata\\s*([^>]*)\\s*>";
  Pattern p2 = Pattern.compile(regEx2);
  Matcher m2 = p2.matcher(temp);
  temp=m2.replaceAll("");
  
  temp = temp.replace("\'", "\"");
  return temp;
 }


说明:
特殊构造(非捕获)
(?:X) X,作为非捕获组
(?idmsux-idmsux)  Nothing,但是将匹配标志由 on 转为 off
(?idmsux-idmsux:X)   X,作为带有给定标志 on - off 的非捕获组
(?=X) X,通过零宽度的正 lookahead
(?!X) X,通过零宽度的负 lookahead
(?<=X) X,通过零宽度的正 lookbehind
(?<!X) X,通过零宽度的负 lookbehind
(?>X) X,作为独立的非捕获组
public static Pattern compile(String regex,int flags);
参数:
regex - 要编译的表达式。
flags - 匹配标志,可能包括 CASE_INSENSITIVE、MULTILINE、DOTALL、UNICODE_CASE 和 CANON_EQ 的位掩码。
Pattern中的定义如下:

Java代码 复制代码
  1. public static final int UNIX_LINES = 0x01;   
  2. public static final int CASE_INSENSITIVE = 0x02;   
  3. public static final int COMMENTS = 0x04;   
  4. public static final int MULTILINE = 0x08;   
  5. public static final int LITERAL = 0x10;   
  6. public static final int DOTALL = 0x20;   
  7. public static final int UNICODE_CASE = 0x40;   
  8. public static final int CANON_EQ = 0x80;  
public static final int UNIX_LINES = 0x01;
public static final int CASE_INSENSITIVE = 0x02;
public static final int COMMENTS = 0x04;
public static final int MULTILINE = 0x08;
public static final int LITERAL = 0x10;
public static final int DOTALL = 0x20;
public static final int UNICODE_CASE = 0x40;
public static final int CANON_EQ = 0x80;


资源:
1.java.util.regex类 Pattern

分享到:
评论

相关推荐

    Packt.Java.9.Regular.Expressions

    Going forward, you will learn to use zero-length assertions and lookarounds, parsing the source code, and processing the log files. Finally, you will master tips, tricks, and best practices in regex ...

    java字符串匹配

    3.Build a program using Java array of string, you need to input 5 or more most famous universities in the world, and the annual ... (Hint: one of solutions is to use java.util.regex API 正则表达式)

    Jeffrey E. F. Friedl - Mastering.Regular.Expressions.3rd.Edition

    this edition has been updated throughout to reflect advances in other languages, including expanded in-depth coverage of Sun's java.util.regex package, which has emerged as the standard Java regex ...

    UE(官方下载)

    "Tagging" the find data allows UltraEdit/UEStudio to re-use the data similar to variable during a replace. For example, If ^(h*o^) ^(f*s^) matches "hello folks", ^2 ^1 would replace it with "folks ...

    正则表达式工具:JGsoft RegexBuddy v3.4.2 零售版(无需要注册激活)

    Use the regex with source code snippets automatically adjusted to the particulars of your programming language. Collect and document libraries of regular expressions for future reuse. GREP (search-...

    Inverted-Index

    倒排索引- unix join - http://www.albany.edu/~ig4895/join.htm- get path-filenames - http://stackoverflow.com/questions/6844785/how-to-use-regex-with-find-command- executing unix commands from java - ...

    带注释的Bootstrap.java

    import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.catalina.Globals; import org.apache.catalina.security.SecurityClassLoad; import org.apache.catalina.startup....

    streamflyer-regex-fast:字符流上的快速匹配

    // we use FastRegexModifier instead of RegexModifier Modifier fastModifier = new FastRegexModifier("edit(\\s+)stream", Pattern.DOTALL, "modify$1stream"); // create the modifying reader that wraps the ...

    Android代码-口语化的正则表达式

    You can use SNAPSHOT dependency with adding to pom.xml: ossrh https://oss.sonatype.org/content/repositories/snapshots Examples VerbalExpression testRegex = VerbalExpression.regex() .startOfLine...

    JGsoft.RegexBuddy.v3.4.2

    Use the regex with source code snippets automatically adjusted to the particulars of your programming language. Collect and document libraries of regular expressions for future reuse. GREP (search-...

    Mastering Regular Expressions, 3rd Edition

    They are now standard features in a wide range of languages and popular tools, including Perl, Python, Ruby, Java, VB.NET and C# (and any language using the .NET Framework), PHP, and MySQL....

    ZendFramework中文文档

    9.1.2. Why Use Zend_Date? 9.2. 操作理论 9.2.1. 内部(Internals) 9.3. Basic Methods 9.3.1. The current date 9.3.2. Zend_Date by Example 9.3.2.1. Ouput a Date 9.3.2.2. Setting a Date 9.3.2.3. ...

Global site tag (gtag.js) - Google Analytics