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()

猜你喜欢

转载自blog.csdn.net/qq_33605294/article/details/85141586