简体   繁体   中英

Matcher lookingAt method matching empty String

I'm using a Matcher instance to match patterns at the start of some input using the lookingAt() method. lookingAt() is however returning true when I create a Pattern from an empty String and try to match it with a non-empty (trimmed) String.

Firstly, this is as I expect it:

String field = "Dave went to Alabama"

String escapedQuery = Pattern.quote("Dave went");
Pattern pattern = Pattern.compile(escapedQuery, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(field);
// matcher.lookingAt() == true

However, this pattern generated from an empty String also returns true:

String escapedQuery = Pattern.quote("");
Pattern pattern = Pattern.compile(escapedQuery, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(field);
//matcher.lookingAt() == true

All Strings involved have been trimmed before any Patterns are compiled.

Can anyone point me as to what I'm missing here?

Many thanks.

It's a consistency matter. Empty patterns are like empty strings, and if you try

System.out.println( "abc".indexOf("") );

you'll see that this is found at offset 0.

Clearly there is a substring of length 0 to be found at many places within a string of length > 0, and even in a string of length 0!

The empty regular expression matches all strings. If you want a regular expression that matches only the empty string, you'll need something like this:

Pattern pattern = Pattern.compile("^$", Pattern.CASE_INSENSITIVE);

^ matches the beginning of a string, and $ matches the end. The expression ^$ therefore matches strings that begin and then immediately end; ie, the empty string.

Of course there are better ways to match the empty string. These spring to mind:

string.length() == 0
string.equals("");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM