Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > RegEx problem

Reply
Thread Tools

RegEx problem

 
 
Soeren Meyer-Eppler
Guest
Posts: n/a
 
      12-10-2005
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:16 8

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
 
Reply With Quote
 
 
 
 
Soeren Meyer-Eppler
Guest
Posts: n/a
 
      12-10-2005
.... 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
 
Reply With Quote
 
 
 
 
Jean-Francois Briere
Guest
Posts: n/a
 
      12-10-2005
> 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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-10-2005
On Sat, 10 Dec 2005 14:57:03 +0100, Soeren Meyer-Eppler
<Soeren.Meyer-> 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.
 
Reply With Quote
 
=?utf-8?B?5bCP6bG85YS/?=
Guest
Posts: n/a
 
      12-11-2005
since group. ==> ".*" should be "(.*)"

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-16-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments