2

I have been working on this project which requires converting a file to a string and then finding the locations of certain strings within the string. I am trying to use the Pattern and Matcher classes to do this. Please take a look at my code below (java):

String begin = "TOSS-UP" + "\\s*" + j;
String end = "TOSS-UP" + "\\s*" + (j+1);
Pattern beginPattern = Pattern.compile(begin);
Pattern endPattern = Pattern.compile(end);
System.out.println(beginPattern);
Matcher beginMatcher = beginPattern.matcher(input);
Matcher endMatcher = endPattern.matcher(input);
int beginPosition = beginMatcher.start();
int endPosition = endMatcher.start();

where j is a variable in a for loop (that starts out at 1 and works its way up to 24) and input is a string that starts out as such:

ROUND 1 TOSS-UP 1) ... TOSS-UP 2) ... TOSS-UP 3) ... TOSS-UP 4) ...

I need to use regex since every so often this string will have a \n between the TOSS-UP and the number (instead of a simple space).

I'm new to regex, trying to learn what I can off internet examples, and I can't see why this program can't compile. The error eclipse gives me is:

Exception in thread "main" java.lang.IllegalStateException: No match available 
at java.util.regex.Matcher.start 
at Driver.main

Any help fixing the regex would be appreciated. If anyone else has another solution to the problem, please let me know. Thanks!

5
  • 2
    You must call matches() or find() before you can call start(). Javadoc even documents this: Throws IllegalStateException if no match has yet been attempted, or if the previous match operation failed. Javadoc of Matcher says: Once created, a matcher can be used to perform three different kinds of match operations: The matches method [...] The lookingAt method [...] The find method [...].* Commented Jun 13, 2017 at 22:10
  • Even after doing so, I still get the same error. I used the find method. Commented Jun 13, 2017 at 22:12
  • 1
    Did they return true, indicating a successful match? You can't ask for match result values until you have successfully performed a match. Makes sense, right?!? --- It's like asking for an apple, being told that there aren't any, then asking how big they are. It is an unanswerable question, which is why you get an exception. Commented Jun 13, 2017 at 22:15
  • Wait, after using find it started partially working (doesn't go through all of the string, still finds the same error after a while, but works for some of it). Let me go through and try to understand why it's stopping at the point that it is and I'll get back to you. Commented Jun 13, 2017 at 22:17
  • After some quick modifications, looks like it's working! Commented Jun 13, 2017 at 22:25

1 Answer 1

2

Call the matches() function first, before trying to get anything from the matcher object. Calling matches() will trigger the match, and a boolean value returned depending on whether there was a match or not.

... // as usual
int beginPosition = -1;
int endPosition = -1;
if(beginMatcher.matches()) {
     beginPosition = beginMatcher.start();
} 

if(endMatcher.matches()) {
    endPosition = endMatcher.start();
}

As pointed out by @MikeSamuel in the comments, if you want only the first match, perhaps .find() would be better. Also, consider adding a \b to the end of your regex so TOSS-UP 1 is not found in TOSS-UP 10, for example.

Sign up to request clarification or add additional context in comments.

1 Comment

.matches() matches the entire string. Maybe find() would be better though in that case you would probably want a \b after the number so that TOSS-UP 1 is not found in TOSS-UP 10.

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.