util.regex.Pattern anomaly

R

Roger Marquis

Finally figured out an odd behavior in 1.4's util.regex.Pattern
class. Specifically, the matcher() method does not work as expected.
Unlike Perl, grep, sed, and most other regex engines Java's
matcher() must match the _entire_ line to return true.

The documentation does have a section titled "Comparison to Perl
5" but for some reason it doesn't list this important difference
(though it does provide a hint in the MULTILINE detail). The fix
is inelegant but straightforward: simply add a wildcard (.*) prefix
and/or suffix to each Pattern.compile. For example the perl/sed/grep:

/somestring/

is equivalent to Java's:

/.*somestring.*/

Here's an example in this hostname validation method:

public static boolean isValidFQDN(String FQDN) {
Pattern legal = Pattern.compile("[0-9a-zA-Z\\.\\-]+");
Matcher isLegal = legal.matcher(FQDN);
if ( ! isLegal.matches() ) {
return false;
}
Pattern illegal = Pattern.compile(".*\\.\\..*|.*--.*|^\\..*|^-.*|.*\\.$|.*-$");
// -- note java regex wildcards ---^^------^^-^^--^^-----^^---^^-^^-----^^
Matcher isIllegal = illegal.matcher(FQDN);
if ( isIllegal.matches() ) {
return false;
}
return true;
}
 
R

Roger Marquis

Roger Marquis said:
Pattern illegal = Pattern.compile(".*\\.\\..*|.*--.*|^\\..*|^-.*|.*\\.$|.*-$");
// -- note java regex wildcards ---^^------^^-^^--^^-----^^---^^-^^-----^^

That should read:

Pattern illegal = Pattern.compile(".*\\.\\..*|.*--.*|^\\..*|^-.*|.*\\.$|.*-$");
// -- note java regex wildcards ---^^------^^-^^--^^-----^^---^^-^^-----^^
 
A

Alan Moore

A much better fix is to use find() instead of matches(). It looks for
a match *within* the target string, like Perl, instead of trying to
match the whole string.
 
R

Roger Marquis

Tor Iver Wilhelmsen said:
It seems you read Pattern's docs but not Matcher's - there are more
methods in that class than the one used by the example in Pattern's
javadoc.

That was the problem. That and a lack of of online documentation.
Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top