开发者

Java Regex won't match, any explanations?

The regex String :

"[Ff][uU][Nn][Cc] " 

Matches input:

"fUnC " 

But not:

"func across( a, b )"

And I don't understand why...

I'm testing my expressions here: http://www.regexplanet.com/simple/index.html

I figured out that I (dumbly) needed my regex to be "[Ff][uU][Nn][Cc] .*" for a match.

SOLVED: Don't use the convenience method Pattern.Matches(regex, input) if you are looking for what amounts to a submatc开发者_高级运维h. You should use the Matcher.find() method instead.


When I use the regex tester you link to, I see that your regex works with find(), but not with matches(). This is what I would expect -- find() just looks for a regex hit within the target string, while matches() always tries to match the entire string.


"[Ff][uU][Nn][Cc].*" may help...


It can be.... it's working fine. But your strings in there and you'll see MATCHES is false, but replaceFirst and ReplaceAll work fine.

If you want MATCHES to be true

add a * at the end


Have you also tried using the regex tester, ignoring case? There should be a way to turn on case insensitivity in the Java regex matcher.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜