Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - String eachMatch() method



eachMatch() method processes each regex group (see next section) matching substring of the given String.

Syntax

void eachMatch(String regex, Closure clos)

Parameters

  • Regex − The string expression to search for.

  • Closure − optional closure.

Return Value

This methods does not return any value.

Example - Printing each character of a string

Following is an example of the usage of eachMatch() method to print each characters.

Example.groovy

class Example {
   static void main(String[] args) {
      String s = "HelloWorld";
      
      s.eachMatch(".") {
         ch -> println ch
      }
   }
}

Output

When we run the above program, we will get the following result −

H
e
l
l
o
W
o
r
l
d

Example - Printing Capital case character of a string

Following is an example of the usage of eachMatch() method to print only capital letters.

Example.groovy

class Example {
   static void main(String[] args) {
      String s = "HelloWorld";
      
      s.eachMatch("[A-Z]") {
         ch -> println ch
      }
   }
}

Output

When we run the above program, we will get the following result −

H 
W

Example - Printing Lower case character of a string

Following is an example of the usage of eachMatch() method to print only small case characters.

Example.groovy

class Example {
   static void main(String[] args) {
      String s = "HelloWorld";
      
      s.eachMatch("[a-z]") {
         ch -> println ch
      }
   }
}

Output

When we run the above program, we will get the following result −

e
l
l
o
o
r
l
d
groovy_strings.htm
Advertisements