马士兵 正则表达式的学习(下)

10. replacement

package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//字符串的替换
    	Pattern p=Pattern.compile("java",Pattern.CASE_INSENSITIVE);//启用不区分大小写的匹配
    	Matcher m=p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf");
    	
    	while(m.find()) {
    		p(m.group());
    	}
    }
    
    public static void p(Object o) {
    	System.out.println(o);
    }
}


输出;

java
Java
JAVa
JaVa
JAVA
Java


package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//字符串的替换
    	Pattern p=Pattern.compile("java",Pattern.CASE_INSENSITIVE);//启用不区分大小写的匹配
    	Matcher m=p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf");
    	
    	//全部转换为大写的JAVA
        p(m.replaceAll("JAVA"));
        
        //单数的JAVA串转换为大写的,偶数的JAVA串转换为小写的
    }
    
    public static void p(Object o) {
    	System.out.println(o);
    }
}

输出:

JAVA JAVA JAVA JAVA IloveJAVA you hateJAVA afasdfasdf


package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//字符串的替换
    	Pattern p=Pattern.compile("java",Pattern.CASE_INSENSITIVE);//启用不区分大小写的匹配
    	Matcher m=p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf");
        
        //单数的JAVA串转换为大写的,偶数的JAVA串转换为小写的
    	StringBuffer buf=new StringBuffer();
    	int i=0;
    	while(m.find()) {
    		i++;
    		if(i%2==0) {
    			m.appendReplacement(buf, "java");
    		}else {
    			m.appendReplacement(buf, "JAVA");
    		}
    	}
		m.appendTail(buf);//添加尾巴 afasdfasdf
		p(buf);
    }
    
    public static void p(Object o) {
    	System.out.println(o);
    }
}

输出:


JAVA java JAVA java IloveJAVA you hatejava afasdfasdf



11.group

package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//分组
    	Pattern p=Pattern.compile("(\\d{3,5})[a-z]{2}");//3到5位的数字 两位的小写字母
    	String s="123aa-34345bb-234cc-00";
    	Matcher m=p.matcher(s);
    	while(m.find()) {
    		p(m.group());//直接打印出整个正则表达式所表示的子串
    	}
    }
    
    public static void p(Object o) {
    	System.out.println(o);
    }
}

123aa
34345bb
234cc


继续分组

package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//分组
    	Pattern p=Pattern.compile("(\\d{3,5})([a-z]{2})");//3到5位的数字 两位的小写字母
    	String s="123aa-34345bb-234cc-00";
    	Matcher m=p.matcher(s);
    	while(m.find()) {
    		p(m.group(1));
    	}
    }
    
    public static void p(Object o) {
    	System.out.println(o);
    }
}

输出:

123
34345
234


package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//分组
    	Pattern p=Pattern.compile("(\\d{3,5})([a-z]{2})");//3到5位的数字 两位的小写字母
    	String s="123aa-34345bb-234cc-00";
    	Matcher m=p.matcher(s);
    	while(m.find()) {
    		p(m.group(2));
    	}
    }
    
    public static void p(Object o) {
    	System.out.println(o);
    }
}

输出:

aa
bb
cc


14.qulifiers

package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//qulifiers
    	Pattern p = Pattern.compile("(.{3,10})[0-9]");
		String s = "aaaa5bbbb6";
		Matcher m = p.matcher(s);
		if(m.find())
			p(m.start() + "-" + m.end());
		else 
			p("not match!");
    	}

    
    public static void p(Object o) {
    	System.out.println(o);
    }
}

输出;

0-10


 Possessive修改:

package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//qulifiers
    	Pattern p = Pattern.compile("(.{3,10}?)[0-9]");
		String s = "aaaa5bbbb6";
		Matcher m = p.matcher(s);
		if(m.find())
			p(m.start() + "-" + m.end());
		else 
			p("not match!");
    	}

    
    public static void p(Object o) {
    	System.out.println(o);
    }
}

输出:

0-5

package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//qulifiers
    	Pattern p = Pattern.compile("(.{3,10}+)[0-9]");
		String s = "aaaa5bbbb6";
		Matcher m = p.matcher(s);
		if(m.find())
			p(m.start() + "-" + m.end());
		else 
			p("not match!");
    	}

    
    public static void p(Object o) {
    	System.out.println(o);
    }
}

输出:

not match!


再度修改:

package com.zzk.cn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
   	
    	//qulifiers
    	Pattern p = Pattern.compile("(.{3,10}+)[0-9]");
		String s = "aaaa5bbbb69";
		Matcher m = p.matcher(s);
		if(m.find())
			p(m.start() + "-" + m.end());
		else 
			p("not match!");
    	}

    
    public static void p(Object o) {
    	System.out.println(o);
    }
}

输出:

0-11



说明:

           greedy贪婪的,一次吞进10个字符,最后的字母没有,一看不行,往外吐一个,正则已经匹配了,到此为止发现已经找到了,正则结束。(默认使用greedy)

           Reluctant勉强的,往往吃最少的,先吃进3个,发现后面不是数字,仍是a,则再吃一个a,发现后面是5,是数字,就不吃了,正则结束。

           Possessive,和贪婪的类似,吃进去不往外吐,都没有的情况下匹配不上。追求效率的时候用。





你可能感兴趣的:(java,String,正则表达式,object,Class)