Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java regex stop after first occurrence

807603Feb 26 2008 — edited Feb 26 2008
When using code like the following:

while (matcher.find()) {
string1=matcher.group(1).trim();
System.out.println(charset);
}

the program goes on looking all through the input string and prints out the final match.

What should be done to find the first occurrence and to stop searching through the input string after the first match has been found? i.e. I want to exit the while loop after the first match is found.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 25 2008
Added on Feb 26 2008
8 comments
813 views