#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Need help understanding illegal state exception from regex


    I compiled two patterns ("\\{(.*)\\}") and ("\\}(.*)").

    The double backslashes are because i'm using a Perl 5 regular expression syntax.

    I'm using matcher(body).group(1) to return the string inside of the parentheses, but it keeps giving me an illegal state exception error and declares that no match was found.

    a sample string would be:

    "{oli22} potatoes are cool"

    In this I would want to return "oli22" and "potatoes are cool".

    Thanks for any help,
    kornkorn
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    I tried this test program.

    Code:
    import java.util.regex.*;
    
    public class Test4 {
    
       public static void main(String[] args) {
          Pattern pattern = Pattern.compile("\\{(.*)\\}");
    
          Matcher matcher = pattern.matcher("{oli22} potatoes are cool");
    
          while (matcher.find())
             System.out.println(matcher.group(1));
    
          pattern = Pattern.compile("\\}(.*)");
    
          matcher = pattern.matcher("{oli22} potatoes are cool");
    
          while (matcher.find())
             System.out.println(matcher.group(1));
       }
    
    }
    and I got this output

    oli22
    potatoes are cool
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    thank you


    Thanks for the reply. I think I spotted what I did wrong in my code. Misusing .find()

IMN logo majestic logo threadwatch logo seochat tools logo