Java正则表达式的简单应用

正则表达式基本内容

转义字符:比如要使用\d匹配一个数字、正则表达式中应当加上'\'反斜杠,即\\d

字符和字符类:(.)匹配任意字符、(\s)匹配空白字符等

定制字符类:[....]可以实现定制化字符类

位置标记:^ $ \b \B

多次、分组、捕获组、编号、交替、特殊选项、贪心程度、预处理模式和后处理模式等

附上Pat大师的 demo :)

package com.zhanxc.regex;

import java.util.*;
import java.util.regex.*;

public class Template 
{
	Properties values = new Properties();
	Pattern templateComment = 
		Pattern.compile("(?si)<!--\\s*TEMPLATE:(\\w+).*?-->");

	public void set( String name, String value	) {
		values.setProperty( name, value );
	}

	public String fillIn( String text ) {
		Matcher matcher = templateComment.matcher( text );

		StringBuffer buffer = new StringBuffer();
		while( matcher.find() ) {
			String name = matcher.group(1);
			String value = values.getProperty( name );
			matcher.appendReplacement( buffer, value );
		}
		matcher.appendTail( buffer );
		return buffer.toString();
	}

	public static void main( String	[] args	) 
	{
		String templateText = 
			"<html><head>\n"+
			"<body>\n"+
			"This is some text.\n"+
			"<!-- TEMPLATE:foo  -->\n"+
			"Some more text.\n"+
			"\n"+
			"<!--template:bar This is text -->\n"+
			"More text.\n"+
			"<!-- TEMPLATE:bar \n"+
			"-->\n"+
			"</body></html>\n";
			
		Template template = new Template();
		template.set( "foo", "FooTemplate");
		template.set( "bar", "BarTemplate");
		System.out.println( templateText );
		System.out.println( template.fillIn(templateText) );
	}

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值