虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > Java编程 > java中regex用法实例

java中regex用法实例
类别:Java编程   作者:码皇   来源:互联网   点击:

importjava util regex *;publicclassMain{publicstaticvoidmain(String[]args)throwsException{Stringtext="testa= "1 "b= "2 "c= "3 "bard= "4 "e= "5 "";System out println(text+" n");Matcherm1=Pattern co
  1. import java.util.regex.*; 
  2.  
  3. public class Main { 
  4.   public static void main(String[] args) throws Exception { 
  5.  
  6.     String text = "test a=\"1\" b=\"2\" c=\"3\" bar d=\"4\" e=\"5\""
  7.     System.out.println(text + "\n"); 
  8.  
  9.     Matcher m1 = Pattern.compile("([a-z]*)((?:[ \t]+[a-z]=\"[0-9]\")*)").matcher(text); 
  10.  
  11.     while(m1.find()) { 
  12.  
  13.       System.out.println(m1.group(1)); 
  14.  
  15.       Matcher m2 = Pattern.compile("([a-z])=\"([0-9])\"").matcher(m1.group(2)); 
  16.  
  17.       while (m2.find()) { 
  18.         System.out.println("  " + m2.group(1) + " -> " + m2.group(2)); 
  19.       } 
  20.     } 
  21.   } 
  1. which produces: 
  2.  
  3. test a="1" b="2" c="3" bar d="4" e="5" 
  4.  
  5. test 
  6.   a -> 1 
  7.   b -> 2 
  8.   c -> 3 
  9.  
  10. bar 
  11.   d -> 4 
  12.   e -> 5 

 

相关热词搜索: 实例 java regex