JAVA 正则表达式

fanchunnyu 2015-04-15 04:16:33
String txt = "{{name}}佛顶山{{creatorId}}你好{{ownerId}}allen{{props.mobile}}设计{{props.telphone}}";
如何使用java正则表达式取出 {{ }}中的字段
...全文
234 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2015-04-16
  • 打赏
  • 举报
回复
String txt = "{{name}}佛顶山{{creatorId}}你好{{ownerId}}allen{{props.mobile}}设计{{props.telphone}}";
		Matcher m = Pattern.compile("\\{\\{(.*?)\\}\\}").matcher(txt);
		while (m.find()) {
			System.out.println(m.group(1));
		}
name creatorId ownerId props.mobile props.telphone
sunshiwu1984 2015-04-16
  • 打赏
  • 举报
回复
String txt = "{{name}}佛顶山{{creatorId}}你好{{ownerId}}allen{{props.mobile}}设计{{props.telphone}}"; Pattern p = Pattern.compile("[{]{2}[\\s\\S]*?[}]{2}"); //用Pattern类的matcher()方法生成一个Matcher对象 Matcher m = p.matcher( txt); while(m.find()){ String s1 = m.group(); s1= s1.replaceAll("([{]{2}|[}]{2})", ""); System.out.println(s1); } 我试了可以的。。
liangtu 2015-04-16
  • 打赏
  • 举报
回复

import org.apache.commons.lang.StringUtils;

String testStr = "{{name}}佛顶山{{creatorId}}你好{{ownerId}}allen{{props.mobile}}设计{{props.telphone}}";
		String[] arr = StringUtils.substringsBetween(testStr, "{{", "}}");
		for (int i = 0; i < arr.length; i++)
		{
			System.out.println(i + " = " + arr[i]);
		}
scmod 2015-04-15
  • 打赏
  • 举报
回复
\\{\\{([\\d\\w\\.]+)\\}\\} 分组,然后拿group(1)就行了 这个看着有点像angularJS什么的
fanchunnyu 2015-04-15
  • 打赏
  • 举报
回复
难道都没有人帮忙解决一下吗??呜呜。。。。。。。。

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧