Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RegEx problem

1 view
Skip to first unread message

Soeren Meyer-Eppler

unread,
Dec 10, 2005, 8:57:03 AM12/10/05
to
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

--
http://www.BuschnicK.net

Soeren Meyer-Eppler

unread,
Dec 10, 2005, 9:06:00 AM12/10/05
to
... 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

unread,
Dec 10, 2005, 3:26:43 PM12/10/05
to
> 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

Roedy Green

unread,
Dec 10, 2005, 5:13:55 PM12/10/05
to
On Sat, 10 Dec 2005 14:57:03 +0100, Soeren Meyer-Eppler
<Soeren.Me...@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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

小鱼儿

unread,
Dec 11, 2005, 8:50:36 AM12/11/05
to
since group. ==> ".*" should be "(.*)"

Robert Klemme

unread,
Dec 16, 2005, 3:58:18 AM12/16/05
to
Soeren Meyer-Eppler wrote:
> ... 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

0 new messages