Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Schrodinger's regular expression

Thread view: 
Christoph - 03 Apr 2007 15:44 GMT
I have a block of code that can be summed up like this

Pattern pat = Pattern.compile("...");
Matcher m = pat.matcher("...");
System.out.println(m.matches());
System.out.println(m.group(1));

The expression matches. But if I comment out the third line (which
only prints "true" and is used for debugging), the group() call
triggers an IllegalStateException - "No match found". If I put it back
in, it works. In a manner of speaking, I need to check whether the
regular expression matches before it actually does.

>From reading the documentation (if superficially), I was under the
impression that the Regular Expression was processed as soon as the
object was constructed with matcher() - is this not true? Does the
boolean function matches() do something besides returning the boolean
value that *has* to be done before I can access the subgroups?
Tom Hawtin - 03 Apr 2007 16:10 GMT
>>From reading the documentation (if superficially), I was under the
> impression that the Regular Expression was processed as soon as the
> object was constructed with matcher() - is this not true? Does the
> boolean function matches() do something besides returning the boolean
> value that *has* to be done before I can access the subgroups?

"public boolean matches()

    "Attempts to match the entire region against the pattern."

The returned value is not the primary function of the operation. It's
mostly about finding a match. It's not well named.

Going back to the top of the API docs for Matcher, there are three kinds
of match operation. matches represents one of these operations. You need
to do some sort of operation before you can start looking at the results
from it.

Tom hawtin
Christoph Burschka - 05 Apr 2007 15:04 GMT
Tom Hawtin schrieb:

>>> From reading the documentation (if superficially), I was under the
>>
[quoted text clipped - 16 lines]
>
> Tom hawtin

So every Matcher needs to be first constructed by Pattern.matcher() and
then have matches() (or one of the others you mentioned) called on it
before use? Got it, thanks. :)

--cb
Eric Sosman - 05 Apr 2007 17:52 GMT
Christoph Burschka wrote On 04/05/07 10:04,:
> Tom Hawtin schrieb:
>
[quoted text clipped - 22 lines]
> then have matches() (or one of the others you mentioned) called on it
> before use? Got it, thanks. :)

   "Before use" isn't how I'd put it, since the action of
checking for a match is surely a "use."  It's probably better
to think of the matching action as the "use," after which
you can call things like group() to get information about
what was matched.

   Weak analogy: The regular expression is "cabbage" and
the matcher you construct from it is a shopping list, but
you must take the list to the market and "match" it in the
produce section before you can have cole slaw.

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.