-1

I have a Matcher for which find() returns true, but despite this, the start(), end(), and group() methods all return IllegalStateException. Here's an extract of the code:

Pattern xmlPattern = Pattern.compile("(<show.*?>.*?</show>)", Pattern.DOTALL);

StringBuffer buf = new StringBuffer("<broadway><show><hi/></show></broadway>");

Matcher m = xmlPattern.matcher(buf);
if (m.find()) {
  returnString = m.group(1);
}

Even though m.find() is true, the Matcher behaves as though the pattern hasn't been found: group() throws an IllegalStateException.

Supposedly this shouldn't be possible. Any ideas?

Elly
  • 63
  • 8
  • 4
    Mandatory link: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454. Use parser to parse XML and avoid troubles. – Pshemo Nov 23 '22 at 14:38
  • You mention `start()` etc. but aren't showing how exactly you're calling them. Please provide a [mcve] and don't make us guess. – Thomas Nov 23 '22 at 14:43
  • I'm not trying to parse the XML. I'm trying to peel off a tag which will then be parsed. The idea is to give an example of how to program for regex in Java, not to process XML per se. – Elly Nov 23 '22 at 14:49
  • Okay, I've supplemented the original query by adding the follow-up to the loop above in code. But the Eclipse debugger makes clear that all three functions throw the Exception. – Elly Nov 23 '22 at 14:51
  • `buf.append(input)` is wrong, it should be `buf.append(input, 0, charsRead)`. Also is there a specific reason you use `StringBuffer` instead of `StringBuilder`? – Joachim Sauer Nov 23 '22 at 15:24
  • Thanks for the tip regarding append(). As far as StringBuffer, I'm just accustomed to it. – Elly Nov 23 '22 at 17:27
  • BTW if you want to access whole match you don't need to wrap entire regex with `(..)` to create indexed/capturing group. Regex engine places whole match in group 0 *by default*, so instead of `group(1)` you can use either `group(0)` or `group()` (which are the same since internally `group()` just calls `group(0)`). – Pshemo Nov 24 '22 at 15:40
  • Thanks for the tip regarding group(). Actually, that's what I ended up with. I don't know what you're referring to by "wrap entire regex with (..)," as I don't see my code snippet doing that. If it's Pattern.DOTALL, that's to accommodate input that spans lines of text. – Elly Nov 27 '22 at 06:47

1 Answers1

1

Thank you all for your help. The most useful response was the one that suggested I add a debugging loop prior to the loop in my query, to display m.group() as long as m.find() was true. It made it through the test loop without a problem.

I ultimately realized that the difference was that there was no breakpoint in the test loop. I tried the program without the debugger and it worked fine.

So I must sheepishly admit that the cause was the fact that I'd added "m.matches()" as a watch expression in the debugger. When execution paused, this expression was false (the pattern didn't match the entire input), and this reset the Matcher's state so start(), end(), and group() threw IllegalStateException.

Elly
  • 63
  • 8