Java pattern.matcher() 与 pattern.matches()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3862917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pattern.matcher() vs pattern.matches()
提问by samarjit
I am wondering why the results of the java regex pattern.matcher() and pattern.matches() differ when provided the same regular expression and same string
我想知道当提供相同的正则表达式和相同的字符串时,为什么 java regex pattern.matcher() 和 pattern.matches() 的结果不同
String str = "hello+";
Pattern pattern = Pattern.compile("\+");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println("I found the text " + matcher.group() + " starting at "
+ "index " + matcher.start() + " and ending at index " + matcher.end());
}
System.out.println(java.util.regex.Pattern.matches("\+", str));
The result of the above are:
上面的结果是:
I found the text + starting at index 5 and ending at index 6
false
I found that using an expression to match the full string works fine in case of matches(".*\\+")
.
我发现使用表达式来匹配完整字符串在matches(".*\\+")
.
回答by Andreas Dolk
pattern.matcher(String s)
returns a Matcher
that can findpatterns in the String s
. pattern.matches(String str)
tests, if the entire String (str
) matches the pattern.
pattern.matcher(String s)
返回Matcher
可以在 String 中找到模式的s
。pattern.matches(String str)
测试,如果整个 String ( str
) 匹配模式。
In brief (just to remember the difference):
简而言之(只是为了记住区别):
pattern.matcher
- test if the string contains-apatternpattern.matches
- test if the string is-apattern
pattern.matcher
- 测试字符串是否包含一个模式pattern.matches
- 测试字符串是否为模式
回答by Buhake Sindi
Matcher.find()
attempts to find the next subsequence of the input sequence that matches the pattern.
Matcher.find()
尝试找到与模式匹配的输入序列的下一个子序列。
Pattern.matches(String regex, CharSequence input)
compiles the regex into a Matcher
and returns Matcher.matches()
.
Pattern.matches(String regex, CharSequence input)
将正则表达式编译为 aMatcher
并返回Matcher.matches()
.
Matcher.matches
attempts to match the entireregion (string) against the pattern (Regex).
Matcher.matches
尝试将整个区域(字符串)与模式(Regex)进行匹配。
So, in your case, the Pattern.matches("\\+", str)
returns a false since str.equals("+")
is false.
因此,在您的情况下,Pattern.matches("\\+", str)
返回 false,因为它str.equals("+")
是 false。
回答by greuze
Pattern.matches is testing the whole String, in your case you should use:
Pattern.matches 正在测试整个字符串,在你的情况下你应该使用:
System.out.println(java.util.regex.Pattern.matches(".*\+", str));
Meaning any string and a + symbol
表示任何字符串和 + 符号
回答by Martijn Verburg
From the Javadoc, see the if, and only if, the entire regionsection
从 Javadoc 中,查看当且仅当整个区域部分
/**
* Attempts to match the entire region against the pattern.
*
* <p> If the match succeeds then more information can be obtained via the
* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods. </p>
*
* @return <tt>true</tt> if, and only if, <b>the entire region</b> sequence
* matches this matcher's pattern
*/
public boolean matches() {
return match(from, ENDANCHOR);
}
So if your String was just "+", you'd get a true result.
所以如果你的字符串只是“+”,你会得到一个真实的结果。
回答by Alan Moore
I think your question should really be "When should I use the Pattern.matches()
method?", and the answer is "Never." Were you expecting it to return an array of the matched substrings, like .NET's Matches
methods do? That's a perfectly reasonable expectation, but no, Java has nothing like that.
我认为你的问题应该是“我Pattern.matches()
什么时候应该使用该方法?”,答案是“从不”。您是否希望它像 .NET 的Matches
方法那样返回匹配子字符串的数组?这是一个完全合理的期望,但不,Java 没有类似的东西。
If you just want to do a quick-and-dirty match, adorn the regex with .*
at either end, and use the string's own matches()
method:
如果您只想进行快速和肮脏的匹配,请.*
在两端装饰正则表达式,并使用字符串自己的matches()
方法:
System.out.println(str.matches(".*\+.*"));
If you want to extract multiple matches, or access information about a match afterward, create a Matcher instance and use itsmethods, like you did in your question. Pattern.matches()
is nothing but a wasted opportunity.
如果您想提取多个匹配项,或之后访问有关匹配项的信息,请创建一个 Matcher 实例并使用其方法,就像您在问题中所做的那样。 Pattern.matches()
只不过是浪费了机会。
回答by Dhaval
matches tries to match the expression against the entire string. Meaning, it checks whether the entire string is a patern or not. conceptually think it like this, it implicitly adds a ^ at the start and $ at the end of your pattern.
匹配尝试将表达式与整个字符串进行匹配。意思是,它检查整个字符串是否是一个模式。从概念上来说,它是这样认为的,它在模式的开头和 $ 隐含地添加了一个 ^ 。
For, String str = "hello+", if you want matches() to return true, you need to have pattern like ".\+."
对于,String str = "hello+",如果你想让matches()返回true,你需要有像“. \+.”这样的模式
I hope this answered your question.
我希望这回答了你的问题。
回答by Nambi_0915
Matcher matcher = pattern.matcher(text);
In this case, a matcher object instance will be returned which performs match operations on the input text by interpreting the pattern. Then we can use,matcher.find()
to match no. of patterns from the input text.
在这种情况下,将返回一个匹配器对象实例,它通过解释模式对输入文本执行匹配操作。然后我们可以使用,matcher.find()
来匹配 no。输入文本中的模式。
(java.util.regex.Pattern.matches("\+", str))
Here, the matcher object will be created implicitly and a boolean will be returned which matches the whole text with the pattern. This will work as same as the str.matches(regex)
function in String.
在这里,将隐式创建匹配器对象,并返回一个布尔值,该布尔值将整个文本与模式相匹配。这将与str.matches(regex)
String 中的函数一样工作。
回答by Maurice Perry
The code equivalent to java.util.regex.Pattern.matches("\\+", str)
would be:
等效于的代码java.util.regex.Pattern.matches("\\+", str)
是:
Pattern.compile("\+").matcher(str).matches();
method find
will find the first occurrence of the pattern in the string.
方法find
将查找字符串中第一次出现的模式。