Método MatchResult start (int) em Java com exemplos
O método start (int group) da Interface MatchResult é usado para obter o índice inicial do resultado da partida já feito, a partir do grupo especificado.
Sintaxe:
public int start(int group)
Parâmetros: este método usa um grupo de parâmetros que é o grupo do qual o índice inicial do padrão combinado é necessário.
Valor de retorno: Este método retorna o índice do primeiro caractere correspondido no grupo especificado.
Exceção: este método gera:
- IllegalStateException se nenhuma correspondência ainda foi tentada ou se a operação de correspondência anterior falhou.
- IndexOutOfBoundsException se não houver grupo de captura no padrão com o índice fornecido.
Os exemplos abaixo ilustram o método MatchResult.start():
Exemplo 1:
// Java code to illustrate start() method
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
// Get the regex to be checked
String regex = "(G*s)";
// Create a pattern from regex
Pattern pattern
= Pattern.compile(regex);
// Get the String to be matched
String stringToBeMatched
= "GeeksForGeeks";
// Create a matcher for the input String
MatchResult matcher
= pattern
.matcher(stringToBeMatched);
while (((Matcher)matcher).find()) {
// Get the first index of match result
System.out.println(matcher.start(1));
}
}
}
Saída:
4 12
Exemplo 2:
// Java code to illustrate start() method
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
// Get the regex to be checked
String regex = "(G*G)";
// Create a pattern from regex
Pattern pattern
= Pattern.compile(regex);
// Get the String to be matched
String stringToBeMatched
= "GFGFGFGFGFGFGFGFGFG";
// Create a matcher for the input String
MatchResult matcher
= pattern
.matcher(stringToBeMatched);
while (((Matcher)matcher).find()) {
// Get the first index of match result
System.out.println(matcher.start(0));
}
}
}
Saída:
0 2 4 6 8 10 12 14 16 18
=
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva