RegEx problem

Discussion in 'Java' started by Soeren Meyer-Eppler, Dec 10, 2005.

  1. 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, Dec 10, 2005
    #1
    1. Advertising

  2. .... 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
    Soeren Meyer-Eppler, Dec 10, 2005
    #2
    1. Advertising

  3. > 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
    Jean-Francois Briere, Dec 10, 2005
    #3
  4. Soeren Meyer-Eppler

    Roedy Green Guest

    On Sat, 10 Dec 2005 14:57:03 +0100, Soeren Meyer-Eppler
    <> 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.
    Roedy Green, Dec 10, 2005
    #4
  5. since group. ==> ".*" should be "(.*)"
    =?utf-8?B?5bCP6bG85YS/?=, Dec 11, 2005
    #5
  6. 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
    Robert Klemme, Dec 16, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    688
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,604
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    589
  4. Xah Lee
    Replies:
    1
    Views:
    927
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    727
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page