avgvstvs
avgvstvs

Reputation: 6315

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 submatch. You should use the Matcher.find() method instead.

Upvotes: 1

Views: 1178

Answers (4)

jefflunt
jefflunt

Reputation: 33954

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.

Upvotes: 0

MJB
MJB

Reputation: 9389

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

Upvotes: 0

njudge
njudge

Reputation: 177

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.

Upvotes: 4

Vicente Plata
Vicente Plata

Reputation: 3380

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

Upvotes: 0

Related Questions