|
Description
|
A DESCRIPTION OF THE REQUEST :
The String API should include a wrapper for the find() method from java.util.regex.Matcher, like it does for the matches(), replaceFirst(), replaceAll() and split() methods. Also, the API doc for the matches() method should emphasize that it tries to match the whole string, and if users want the traditional "match" functionality, they should use find() instead.
JUSTIFICATION :
People who have used regular expressions in other tools and languages like Perl, sed, or Python expect "matches" to mean the same thing it did there: that some *substring* of the target matches the regex. But if they try to use String's matches() method that way, they get a nasty surprise, because it tries to match the *whole* target. If they go and look at the API docs for the java.util.regex package, they'll eventually figure out that find() is what they want--but they have to create a Pattern and Matcher to use it. It's like a bait-and-switch scam.
The "match" function, as defined in virtually every other regex tool, is by far the most common way that regular expresions are used. It's also the one regex-related method that wasn't added to the String API. If only one of the two methods, matches() or find(), could be added to String, it should have been the more generally useful find().
CUSTOMER SUBMITTED WORKAROUND :
The workaround that's usually suggested in the JDC Forums and Usenet newsgroups is to add ".*" to the beginning of the regex, but that's only useful *after* you've realized that matches() isn't working the way you expected it to. Most people seem to spend several hours trying to figure out what's wrong with their regex first. I want to spare people that frustration.
(Incident Review ID: 199559)
======================================================================
Posted Date : 2006-07-21 06:59:53.0
|