Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4908449
Votes 3
Synopsis (str) String.find(String regex)
Category java:classes_lang
Reported Against 1.4.2
Release Fixed
State 6-Fix Understood, request for enhancement
Priority: 4-Low
Related Bugs
Submit Date 18-AUG-2003
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
Work Around
N/A
Evaluation
This does seem compelling although it is apparently too late to add this method in the Tiger release.
  xxxxx@xxxxx   2003-08-29
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang