Schrodinger's regular expression

C

Christoph

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?
 
T

Tom Hawtin

Christoph said:
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
 
C

Christoph Burschka

Tom said:
"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

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
 
E

Eric Sosman

Christoph Burschka wrote On 04/05/07 10:04,:
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. :)

"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.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top