RegEx problem

  • Thread starter Soeren Meyer-Eppler
  • Start date
S

Soeren Meyer-Eppler

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
 
S

Soeren Meyer-Eppler

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

Jean-Francois Briere

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
 
R

Robert Klemme

Soeren said:
... 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
 

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

Similar Threads

regex problem 9
Java Regex Problem 7
complex regex 1
complex regex 1
regex group(1) problem 2
Grouping Regex question 2
Regex: Capturing and replacing question 9
funky but simple regex 3

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top