Solved

Matcher.start(int group)

Posted on 2007-04-03
11
285 Views
Last Modified: 2013-12-29
Hi Experts ..

java.util.regex.Matcher.start(int group) method throws IllegalStateException if "If no match has been attempted, or if the previous match operation failed."

could someone please give me example for that case??
0
Comment
Question by:DrAske
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 3

Expert Comment

by:cavey_79
Comment Utility
When you just create the matcher, without calling methods like matches, or when you call them and they return false.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 165 total points
Comment Utility
Pattern p = Pattern.compile("(\\d)");
Matcher m = p.matcher("abc");
m.start(1);
0
 
LVL 3

Expert Comment

by:cavey_79
Comment Utility
In other words, you will need to successfully do one of these three operations:
      The matches method attempts to match the entire input sequence against the pattern.
      The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.
      The find method scans the input sequence looking for the next subsequence that matches the pattern.

before you can call start, group, etc.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
There are no digits in "abc" so the match will fail and thus the call to start throws the exception
0
 
LVL 92

Assisted Solution

by:objects
objects earned 165 total points
Comment Utility
From the javadoc it "Returns the start index of the subsequence captured by the given group during the previous match operation.".
So if there is no successful previous match operation then it cannot return the start index, in which case it will throw the exception.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 9

Author Comment

by:DrAske
Comment Utility
Thanks for all of you :o) so I can avoid it using find() method,
then the following will not throw the exception cause the while condition will be false

Pattern pattern = Pattern.compile("(\\d)");
Matcher matcher = pattern.matcher("abc");
 while(matcher.find()){
            int start = matcher.start(1);
            int end = matcher.end(1);
}

really appreciate your help,
regards,
0
 
LVL 3

Accepted Solution

by:
cavey_79 earned 170 total points
Comment Utility
If you don't need to match it repeatedly, you can do this:
if (matcher.matches()){
          int start = matcher.start(1);
          int end = matcher.end(1);
}
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
that is correct
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>then the following will not throw the exception cause the while condition will be false

Correct
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0
 
LVL 9

Author Comment

by:DrAske
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
scoresSpecial  challenge 13 40
advertisement module in core php 4 81
JList custom Cell Renderer refresh 15 39
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now