Javatpoint Logo
Javatpoint Logo

Java Matcher group() Method

The group method returns the matched input sequence captured by the previous match in the form of the string. This method returns the empty string when the pattern successfully matches the empty string in the input.

Signature

There are 3 types of group method in java. The signature of group methods are given below

No. Method Description
1 String group() Returns the matched sequence captured by the previous match as the string.
2 String group(int group) Returns the matched sequence captured by the given group during the previous match operation as the string.
3 String group(String name) Returns the matched sequence captured by the given named group during the previous match operation or null if the match fails.

Specified by

The group in interface MatchResult

Returns

The (possibly empty) subsequence matched by the previous match, in string form

Throws

IllegalStateException - If no match has yet been attempted, or if the previous match operation failed.

Example 1

Test it Now

Output:

Start :1, End : 4, Group abb
Start :4, End : 7, Group abb
Start :7, End : 10,Group abb

Example 2

Test it Now

Output:

Group 0 is
Group 1 s
My name is Khan and m not a terrerist.

Example 3

Test it Now

Output:

Hello world

Note: Here the group name java contains all the alphabets both small and capital.


Next TopicJava Matcher class




Help Others, Please Share

facebook twitter pinterest