Java的正则表达式不工作 - 为什么? [英] Java Regex not working - why?

查看:126
本文介绍了Java的正则表达式不工作 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

match.matches()返回false。这很奇怪,因为如果我借这个正则表达式和测试字符串rubular.com,是显示了两场比赛。我在做什么错了?

 模式正则表达式= Pattern.compile(FTW(((ODP))+)ODP?!);
匹配器匹配= regex.matcher(ZZZMMMJJJOOFTWZMJZMJODPZZZMMMJJJOOOFTWMZJOMZJOMZJOODPZZZMMMJJJOO);如果(match.matches()){
的System.out.println(匹配找到);
}
其他{
的System.out.println(匹配未找到);
}


解决方案

<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html#matches%28%29\"><$c$c>Matcher.matches返回是否在全区的匹配模式。

请尝试使用<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html#find%28%29\"><$c$c>find代替。 (当然你的榜样,这工作正常。)

match.matches() returns false. This is odd, because if I take this regex and test String to rubular.com, is shows two matches. What am I doing wrong?

	Pattern regex = Pattern.compile("FTW(((?!ODP).)+)ODP");
	Matcher match = regex.matcher("ZZZMMMJJJOOFTWZMJZMJODPZZZMMMJJJOOOFTWMZJOMZJOMZJOODPZZZMMMJJJOO");

	if (match.matches()) {
		System.out.println("match found");
	}
	else {
		System.out.println("match not found");
	}

解决方案

Matcher.matches returns whether or not the whole region matches the pattern.

Try using find instead. (Certainly with your example, this works fine.)

这篇关于Java的正则表达式不工作 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆