Solved

Matcher.start(int group)

Posted on 2007-04-03
11
297 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
ID: 18846952
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
ID: 18846956
Pattern p = Pattern.compile("(\\d)");
Matcher m = p.matcher("abc");
m.start(1);
0
 
LVL 3

Expert Comment

by:cavey_79
ID: 18846963
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
ID: 18846965
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
ID: 18846989
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Author Comment

by:DrAske
ID: 18847016
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
ID: 18847023
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
ID: 18847026
that is correct
0
 
LVL 86

Expert Comment

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

Correct
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18847047
:-)
0
 
LVL 9

Author Comment

by:DrAske
ID: 18847285
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Core java. Create the class with method that do logic for all elements of collection. 1 69
solarwind tftp server 2 44
backtracking recursion  code 19 53
stackato and cloud 4 72
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

920 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

13 Experts available now in Live!

Get 1:1 Help Now