正则表达式练习

借鉴网上的例子,自己运行通过

引用 http://www.jb51.net/article/31251.htm

        http://blog.youkuaiyun.com/ioe_gaoyong/article/details/7930689



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

public class Regex {

	/** 
	 * @author admin
	 * @date 2013-11-7
	 * @param args
	 * @Description 
	 */
	public static void main(String[] args) {
		/*
		String s = "<img src=\"image/icons/cloudday_small.gif\" width=\"20\" height=\"20\">dfsdf<img src=\"image/icons/cloudday_454.gif\" width=\"20\" height=\"20\">";
		
		Matcher m = Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(s);
		StringBuffer sb2=new StringBuffer(); 
		while(m.find())
		{
		  System.out.println(m.group(1));
		  //s=s.replaceAll(m.group(1), "http://"+m.group(1));
		  m.appendReplacement(sb2, "http://"+m.group(1)); 
		  
		} 
		m.appendTail(sb2); 
		System.out.println(sb2.toString());*/
		
		//s.replace("img", "abc");
		//System.out.println(s);
		/*
		String str="2007年12月11日"; 
		Pattern p = Pattern.compile("[年月日]"); 
		String[] dataArr =p.split(str); 
		for (String strTmp : dataArr) { 
		System.out.println(strTmp); 
		} */
		
		/*
		String input="职务=GM 薪水=50000 , 姓名=职业经理人 ; 性别=男 年龄=45 "; 
		String patternStr="(\\s*,\\s*)|(\\s*;\\s*)|(\\s+)"; 
		Pattern pattern=Pattern.compile(patternStr); 
		String[] dataArr=pattern.split(input); 
		for (String str : dataArr) { 
		System.out.println(str); 
		} 
		*/
		/*
		String regex="<(\\w+)>(\\w+)</(\\w+)>"; 
		Pattern pattern=Pattern.compile(regex); 
		String input="<name>Bill</name><salary>50000</salary><title>GM</title>"; 
		Matcher matcher=pattern.matcher(input); 
		while(matcher.find()){ 
			System.out.println(matcher.groupCount());
			System.out.println(matcher.group(1)); 
		    System.out.println(matcher.group(2)); 
		    System.out.println(matcher.group(3)); 
		   
		} */
		/*
		String regex="([a-zA-Z]+[0-9]+)"; 
		Pattern pattern=Pattern.compile(regex); 
		String input="age45 salary500000 50000 title"; 
		Matcher matcher=pattern.matcher(input); 
		StringBuffer sb=new StringBuffer(); 
		while(matcher.find()){ 
			System.out.println(matcher.groupCount());
		String replacement=matcher.group(1).toUpperCase(); 
		matcher.appendReplacement(sb, replacement); 
		} 
		matcher.appendTail(sb); 
		System.out.println("替换完的字串为"+sb.toString()); */
		
	
       /*
		String str="10元 1000人民币 10000元 100000RMB"; 
		//str=str.replaceAll("(\\d+)(元|人民币|RMB)", "¥"); 
		//System.out.println(str); 
		System.out.println(str.replaceAll("(\\d+)(元|人民币|RMB)", "$1¥")); 
		
		*/
		/*
		String regex="(\\d+)(元|人民币|RMB)"; 
		Pattern pattern=Pattern.compile(regex); 
		String input="10元 1000人民币 10000元 100000RMB"; 
		Matcher matcher=pattern.matcher(input); 
		
		while(matcher.find()){ 
			//System.out.println(matcher.groupCount());
			//System.out.println(matcher.group());
			System.out.println(matcher.group(1));
			System.out.println(matcher.group(2));
			System.out.println();
		
		} */
		/*
		Pattern p=Pattern.compile("([a-z]+)(\\d+)"); 
		Matcher m=p.matcher("aaa2223bb"); 
		System.out.println(m.find());   //匹配aaa2223 
		System.out.println(m.groupCount());   //返回2,因为有2组 
		System.out.println(m.start(1));   //返回0 返回第一组匹配到的子字符串在字符串中的索引号 
		System.out.println(m.start(2));   //返回3 
		System.out.println(m.end(1));   //返回3 返回第一组匹配到的子字符串的最后一个字符在字符串中的索引位置. 
		System.out.println(m.end(2));   //返回7 
		System.out.println(m.group(1));   //返回aaa,返回第一组匹配到的子字符串 
		System.out.println(m.group(2));   //返回2223,返回第二组匹配到的子字符串 

	*/
		/* String email = "abacdemasterhappy@163.com";
		  String regex = "(\\w)+@([\\d\\.\\w])+";
		  Pattern p = Pattern.compile(regex);
		  Matcher m = p.matcher(email);
		  if(m.find()){
		   System.out.println("匹配结果:\t" + m.group(0));
		   System.out.println("分组1:\t" + m.group(1));
		   System.out.println("分组2:\t" + m.group(2));
		   //System.out.println("分组3:\t" + m.group(3));
		   }*/
		
		String s = "abc def".replaceAll("(\\w+)\\s+(\\w+)", "$2 $1"); 
		System.out.println(s);
		
		//String regex="\\(?0\\d{2}[) -]?\\d{8}"; 
		String regex="\\(?0\\d{2}\\)?[- ]?\\d{8}|0\\d{2}[- ]?\\d{8}"; 
		Pattern pattern=Pattern.compile(regex); 
		String input="010)88886666"; 
		Matcher matcher=pattern.matcher(input); 
		
		while(matcher.find()){ 
			System.out.println(matcher.groupCount());
			System.out.println(matcher.group());
			//System.out.println(matcher.group(1));
			//System.out.println(matcher.group(2));
			System.out.println();
		
		}
		/*
		String testStr="The population of 29844442155 is growing";  
//		利用逆向环视功能,非数字字符的结尾  
		  testStr=testStr.replaceAll("(?<=\\d)(?=(\\d\\d\\d)+(?!\\d))",",");  
		System.out.println(testStr);  */
		/*
		String testStr="The population of 298444421569 is growing";  
//		利用正向环视功能,还有用到了反向引用  
		  testStr=testStr.replaceAll("(\\d)(?=(\\d\\d\\d)+(?!\\d))","$1,");  
		System.out.println(testStr);  
		*/
		/*
		String str="2012-8-17,9:30-11:30";  
		str=str.replaceAll("(\\d{4}-\\d{1,2}-\\d{1,2}),(\\d{1,2}:\\d{1,2}-\\d{1,2}:\\d{1,2})",  
		        "$2,$1");  
		System.out.println(str);  */
		
		String str="The population of 2984444215 is growing";  
		Pattern p=Pattern.compile(".*(population\\s+of\\s+)(\\d+).*");  
		Matcher m=p.matcher(str);  
		if(m.find()){  
		    String result=m.group(2); //采用正则的捕获组,确保字符串的位置正确  
		    System.out.println("result is "+result);  
		}  
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值