`
aqbbsxiao
  • 浏览: 45276 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

类似于velocity的正则匹配

    博客分类:
  • 1
阅读更多
public static void main(String[] args) {
		
		Map<String,String> tokens = new HashMap<String,String>();

		tokens.put("cat", "Garfield");
		tokens.put("beverage", "coffee");
		
		//匹配类似velocity规则的字符串
		String template = "$cat really needs some $beverage.";
		//生成匹配模式的正则表达式
//String patternString = "\\$\\{(" + StringUtils.join(tokens.keySet(), "|") + ")\\}";
		String patternString = "\\$(" + StringUtils.join(tokens.keySet(), "|") + ")";
		
		Pattern pattern = Pattern.compile(patternString);
		Matcher matcher = pattern.matcher(template);
		
		//两个方法:appendReplacement, appendTail
		StringBuffer sb = new StringBuffer();
		while(matcher.find()) {
			matcher.appendReplacement(sb, tokens.get(matcher.group(1)));
		}
		matcher.appendTail(sb);
		
		//out: Garfield really needs some coffee.
		System.out.println(sb.toString());
		
		//对于特殊含义字符"\","$",使用Matcher.quoteReplacement消除特殊意义
		matcher.reset();
		//out: cat really needs some beverage.
		System.out.println(matcher.replaceAll("$1"));
		//out: $1 really needs some $1.
		System.out.println(matcher.replaceAll(Matcher.quoteReplacement("$1")));
		
		//到得邮箱的前缀名。插一句,其实验证邮箱的正则多种多样,根据自己的需求写对应的正则才是王道
		String emailPattern = "^([a-z0-9_\\.\\-\\+]+)@([\\da-z\\.\\-]+)\\.([a-z\\.]{2,6})$";
		pattern = Pattern.compile(emailPattern);
		matcher = pattern.matcher("test@qq.com");
		//验证是否邮箱
		System.out.println(matcher.find());
		//得到@符号前的邮箱名  out: test
		System.out.println(matcher.replaceAll("$1"));
		
		//获得匹配值
		String temp = "<meta-data android:name=\"appid\" android:value=\"joy\"></meta-data>";
		pattern = Pattern.compile("android:(name|value)=\"(.+?)\"");
		matcher = pattern.matcher(temp);
		while(matcher.find()) {
			//out: appid, joy
			System.out.println(matcher.group(2));
			}
		}
分享到:
评论

相关推荐

    Velocity模板引擎Velocity模板引擎

    Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎

    velocity-engine-core-2.3-API文档-中英对照版.zip

    赠送jar包:velocity-engine-core-2.3.jar; 赠送原API文档:velocity-engine-core-2.3-javadoc.jar; 赠送源代码:velocity-engine-core-2.3-sources.jar; 赠送Maven依赖信息文件:velocity-engine-core-2.3.pom;...

    Velocity1.4

    Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity

    Velocity 和 FreeMarker区别

    Velocity 和 FreeMarker区别 对于大部分的应用来说,使用 FreeMarker 比 Velocity 更简单,因为 Velocity 还必须编写一些自定义的

    Java进阶教程Velocity快速掌握模板引擎视频

    本课程从velocity engine也就是velocity引擎开始, 先讲解velocity的基本使用以及基础语法 , 然后再讲解velocity 的进阶内容velocity Tools , 以及velocity作为web项目的视图改如何使用 , 每一部分都会有一个综合案例...

    velocity-engine-core-2.3-API文档-中文版.zip

    赠送jar包:velocity-engine-core-2.3.jar 赠送原API文档:velocity-engine-core-2.3-javadoc.jar 赠送源代码:velocity-engine-core-2.3-sources.jar 包含翻译后的API文档:velocity-engine-core-2.3-javadoc-...

    velocity-tools-generic-3.1-API文档-中英对照版.zip

    赠送jar包:velocity-tools-generic-3.1.jar; 赠送原API文档:velocity-tools-generic-3.1-javadoc.jar; 赠送源代码:velocity-tools-generic-3.1-sources.jar; 赠送Maven依赖信息文件:velocity-tools-generic-...

    velocity为空判断

    在使用velocity作为视图层时候,经常会碰到为空的判断,这里整理了velocity为空判断

    Velocity入门教程(Spring集成Velocity)

    Velocity入门教程,语法,Velocity布局,Spring框架集成Velocity

    velocity标签 、velocity教程

    打开网址:http://www.web-tag.net/all_17.htm 就是velocity标签大全 、教程 或下载CHM格式帮助文档

    velocity所需的jar包

    开发velocity所需的jar包

    Velocity-API.chm--采用官方文档制作-超值的Velocity API参考文档。

    当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点,也就是说,页面设计人员可以只 关注页面的显示效果,而由java程序开发人员关注业务逻辑编码。Velocity将java...

    模板:velocity和freemarker的比较

    模板:velocity和freemarker的比较模板:velocity和freemarker的比较

    velocity-1.7-API文档-中文版.zip

    赠送jar包:velocity-1.7.jar; 赠送原API文档:velocity-1.7-javadoc.jar; 赠送源代码:velocity-1.7-sources.jar; 包含翻译后的API文档:velocity-1.7-javadoc-API文档-中文(简体)版.zip 对应Maven信息:...

    Velocity框架学习教程

    Velocity教程

    Velocity+Struts 配置

    Web 设计者可以根据MVC模式和JAVA程序员并行工作,这意味着Web设计者可以单独专注于设计良好的站点,而程序员则可单独专注于编写底层代码。Velocity 将Java 代码从web页面中分离出来,使站点在长时间运行后仍然具有...

    velocity.min.js

    利用velocity来定义动画序列,可以直接使用velocity.js中预定义动画开发自己的网页动画,也可以利用velocity.js自定义动画

    velocity中文文档 教程

    velocity中文文档 教程 velocity中文文档 教程 velocity中文文档 教程

    velocity-tools-2.0.jar

    velocity-tools-2.0.jar velocity-tools-2.0.jar velocity-tools-2.0.jar velocity-tools-2.0.jar velocity-tools-2.0.jar

    velocity的学习资料

    关于velocity的学习资料,velocity教程和velocity用户手册

Global site tag (gtag.js) - Google Analytics