Método Matcher hitEnd() en Java con ejemplos

El método hitEnd() de Matcher Class se usa para verificar si la coincidencia del patrón en este comparador se ha detenido o no. La coincidencia finaliza cuando no se encuentran más grupos coincidentes en el comparador. Este método devuelve un valor booleano que indica lo mismo.

Sintaxis:

public boolean hitEnd()

Parámetros: Este método no toma parámetros.

Valor devuelto: este método devuelve un valor booleano que indica si este comparador ha terminado de emparejar o no.

Los siguientes ejemplos ilustran el método Matcher.hitEnd():

Ejemplo 1:

// Java code to illustrate hitEnd() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "(Geeks)";
  
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
  
        // Get the String to be matched
        String stringToBeMatched
            = "GeeksForGeeks Geeks for For Geeks Geek";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
  
        while (matcher.find()) {
  
            System.out.println("Group matched: "
                               + matcher.group());
  
            // Check if the matching has ended
            // using hitEnd() method
            System.out.println("Has matching hit end: "
                               + matcher.hitEnd());
        }
  
        // Check if the matching has ended
        // using hitEnd() method
        System.out.println("Has matching hit end: "
                           + matcher.hitEnd());
    }
}
Producción:

Group matched: Geeks
Has matching hit end: false
Group matched: Geeks
Has matching hit end: false
Group matched: Geeks
Has matching hit end: false
Group matched: Geeks
Has matching hit end: false
Has matching hit end: true

Ejemplo 2:

// Java code to illustrate hitEnd() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "(GFG)";
  
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
  
        // Get the String to be matched
        String stringToBeMatched
            = "GFGFGFGFGFGFGFGFGFG FGF GFG GFG FGF";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
  
        while (matcher.find()) {
  
            System.out.println("Group matched: "
                               + matcher.group());
  
            // Check if the matching has ended
            // using hitEnd() method
            System.out.println("Has matching hit end: "
                               + matcher.hitEnd());
        }
  
        // Check if the matching has ended
        // using hitEnd() method
        System.out.println("Has matching hit end: "
                           + matcher.hitEnd());
    }
}
Producción:

Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Has matching hit end: true

Referencia: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#hitEnd–

Publicación traducida automáticamente

Artículo escrito por Kirti_Mangal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *