开发者

Java Regex won't match, any explanations?

开发者 https://www.devze.com 2023-02-28 16:56 出处:网络
The regex String : \"[Ff][uU][Nn][Cc] \" Matches input: \"fUnC \" But not: \"func across( a, b )\" And I don\'t understand why...

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

精彩评论

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