1. 程式人生 > >java正則式與反射

java正則式與反射

正則式

  1. 注意 . 和*的用法,還有一些[abc]之類的,需要的話可以查詢手冊
  2. 基本用法Pattern Matcher。
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class Pattern_Matches {
        public static void main(String[] args) {
            Pattern p=Pattern.compile("a.b");
            Matcher m=p.matcher("aaab");
            boolean b=m.matches();
            System.out.println(b);
    		//等效於
    		boolean b=Pattern.matches("a*b","aaab");
        }
    }
    

    還有find() group() split()

    Matcher m1=Pattern.compile("\\w+").matcher("Evenning is full of linnet's wings");
            while(m1.find())
                System.out.print(m1.group()+" ");
            int i=0;
            System.out.println();
            while(m1.find(i)){
                System.out.print(m1.group()+" ");
                i++;
            }
    

    結果

    Evenning is full of linnet s wings

    Evenning venning enning nning ning ing ng g is is s full full ull ll l of of f linnet linnet innet nnet net et t s s wings wings ings ngs gs s

    還有split()注意返回的是陣列。

    String input="hello!!welcome!!thanks!!bye";
            String[] sp=Pattern.compile("!!").split(input);
            System.out.println(Arrays.toString(sp));
            String[] sp1=Pattern.compile("!!").split(input,3);
            System.out.println(Arrays.toString(sp1));
    

    結果

    [hello, welcome, thanks, bye]

    [hello, welcome, thanks!!bye]

     

    類方法提取器,注意使用Clas.ForName在編譯時是不可知的,所以一定要加異常處理,replaceAll在這裡是將method轉為字串,不能直接用toString。

    還有getFields只能得到public的域,private是不可見的。

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.Arrays;
    import java.util.regex.Pattern;
    
    
    public class ShowMethods {
        private String field="Field";
        public static int i=1;
        
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            try {
                
                Class<?> c=Class.forName(args[0]);
                Field[] fields=c.getFields();
                System.out.println(Arrays.toString(fields));
                String name=c.getName();
                System.out.println(name);
                Object o=c.getClass();
                System.out.println(o);
                Method[] methods=c.getMethods();
                Constructor[] cst=c.getConstructors();
                Pattern p=Pattern.compile("\\w+\\.");
                for(Method m:methods){
                    System.out.println(p.matcher(m.toString()).replaceAll(""));
                }
                System.out.println("");
                for(Constructor ct:cst){
                    System.out.println(p.matcher(ct.toString()).replaceFirst(""));
                }
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // TODO Auto-generated method stub
    
        }
    
    }
    

    結果

    [public static int ShowMethods.i]

    ShowMethods

    class java.lang.Class

    public static void main(String[])

    public final void wait(long,int) throws InterruptedException

    public final native void wait(long) throws InterruptedException

    public final void wait() throws InterruptedException

    public boolean equals(Object)

    public String toString()

    public native int hashCode()

    public final native Class getClass()

    public final native void notify()

    public final native void notifyAll()

     

    public ShowMethods()