1

I spent a bit of time looking for the answer, but I couldn't find it, so, here's the question:

I'm getting this error:

    StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw  
    exceptionjava.lang.IllegalStateException: No match found
        at java.util.regex.Matcher.group(Matcher.java:536)
        at extra.regex.checkRegex(regex.java:21)

And here's the code

    public class regex {
        public static String checkRegex(String check, String regex)
        {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(check);
            m.matches();
            return m.group(1);
        }
    }

I'm calling checkRegex() with this:

    String query = request.getQueryString();//this query looks like "sel=45"
    String regulex = "#^(.+)#"; //even this guy returns no matches

    //out.println(query); //it is is not null
    String result = regex.checkRegex(query, regulex);

    out.println(result);

I've seen a few questions here, but all were about missing m.matches() or m.find(), but I've triple checked that.

6
  • 1
    What does matches return? Commented Aug 1, 2015 at 16:51
  • This is explained in the Javadoc: docs.oracle.com/javase/7/docs/api/java/util/regex/… Commented Aug 1, 2015 at 16:55
  • @SotiriosDelimanolis matches() returns a boolean, an extra check needed? @Reimeus, I've checked that via regex101, unfortunately, it matches completely Commented Aug 1, 2015 at 16:57
  • "I've seen a few questions here, but all were about missing m.matches() or m.find()" do you know what is purpose of these methods? Do you know difference between them? Commented Aug 1, 2015 at 16:57
  • regex101 doesn't do matches, it does find. Commented Aug 1, 2015 at 16:58

3 Answers 3

3

You shoud use m.group() in an if block:

     if(m.matches()) {
        return m.group(1);
      } else {
        return null;
      }
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, that fixed the error, but the value is still null
@AlexShereshovets and what schould the result should be?
with this regex - it should return the whole query, I know, this is pointless, but it takes nothing to change the expression)
@AlexShereshovets Post example text you want to parse and show part which you want to find. Also #^ doesn't make sense because ^ represents start of string, which but if it is placed right after # it means that it will never match start of string. This simply can't work.
@Pshemo for example, I want to find 121123123234 in sel=121123123234, so I will use this #[^=]+=([0-9]+)# or #sel=([0-9]+)#, but I tried to make it simpler and throw away cases when my regex could be wrong
|
1

Let's break down your pattern:

"#^(.+)#"
  • #^ - Literal string
  • (.+) - Any character (may or may not match line terminators) 1 or more times
  • # - Literal string

So why does sel=45 fail to match? The String does not begin with "#^" and does not end with "#".

It would seem you want to capture data in between "#" that may or may not be there (You need to clarify this). The following pattern accomplishes that:

"#?([^#]+)#?"

This will match sel=45 and will match #sel=45#

Example:

public static void main(String[] args) throws Exception {
    String regulex = "#?([^#]+)#?"; //even this guy returns no matches

    System.out.println(checkRegex("#sel=45#", regulex));
    System.out.println(checkRegex("sel=45", regulex));
}

public static String checkRegex(String check, String regex) {
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(check);
    return m.matches() ? m.group(1): null;
}

Results:

sel=45
sel=45

1 Comment

Oh, I used # as a delimiter, but it was not required, and this messed it all up. Thank you
0

This regex expression

  String regulex = "^#(.+)#"; 

will match

    String query = "#sel=45#";

This combination doesn't make sense #^. ^ indicates the beginning of string, nothing can be before it. And if you query does not contain # it won't match regulex.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.