Hello,
I have a very frustrating problem getting Javas regexes to run. I have
no idea what I'm doing wrong, since even the most trivial expressions
don't find any matches - even when I've tested and verified them in
other regex enabled tools like UltraEdit or so.
The simplest test I could think of is the following:
public static void main(String[] args)
{
Pattern pattern = Pattern.compile( ".*" );
Matcher matcher = pattern.matcher( "hello" );
System.out.println( matcher.group());
}
which gives me:
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Unknown Source)
at java.util.regex.Matcher.group(Unknown Source)
at BinNavi.GraphNavigator.main(GraphNavigator.java:1688)
WTF?! I mean shouldn't .* match just about anything? Including the empty
string? Similarly, matching "hello" to "hello" doesn't work, "t" to
"t"... Am I using it wrong?
I'm running on Java Version 1.5.0 (Build 1.5.0_06-b05), jdk1.5.0, my
test environment is Eclipse.
I'd appreciate any hints, since I'm sure this is a stupid mistake on my
part.
regards,
Sören Meyer-Eppler

Signature
http://www.BuschnicK.net
Soeren Meyer-Eppler - 10 Dec 2005 14:06 GMT
... argh. Solved the problem. Of course I forgot to call matcher.find().
Although I'm not quite sure why it's necessary since it already knows
the search subject string and could search on construction.
Ah well, sorry about that,
Sören
Jean-Francois Briere - 10 Dec 2005 20:26 GMT
> Although I'm not quite sure why it's necessary
Because you can do other things than find() with a matcher.
So you have to specify to the matcher what you need to do.
Regards
Robert Klemme - 16 Dec 2005 08:58 GMT
> ... argh. Solved the problem. Of course I forgot to call
> matcher.find(). Although I'm not quite sure why it's necessary since
> it already knows the search subject string and could search on
> construction.
1. because there's also matches() as Jean pointed out.
2. you can invoke find multiple times
Kind regards
robert
Roedy Green - 10 Dec 2005 22:13 GMT
On Sat, 10 Dec 2005 14:57:03 +0100, Soeren Meyer-Eppler
<Soeren.Meyer-Eppler@BuschnicK.net> wrote, quoted or indirectly quoted
someone who said :
>I have a very frustrating problem getting Javas regexes to run. I have
>no idea what I'm doing wrong, since even the most trivial expressions
>don't find any matches - even when I've tested and verified them in
>other regex enabled tools like UltraEdit or so.
see some tricks at http://mindprod.com/jgloss/regex.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
小鱼儿 - 11 Dec 2005 13:50 GMT
since group. ==> ".*" should be "(.*)"