Java : MatchResult (regex) con ejemplos

MatchResult (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de MatchResult.

Nota :


Summary

El resultado de una operación de coincidencia. (Traducción automática)

Class diagram

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc 345");

final var stream = matcher.results();

stream.forEach(matchResult -> {

    System.out.println("------");
    System.out.println("start : " + matchResult.start());
    System.out.println("end : " + matchResult.end());
    System.out.println("group : " + matchResult.group());
});

// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 14
//group : aaaabc

Methods

int end ()

Devuelve el desplazamiento después del último carácter coincidente. (Traducción automática)

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc");

System.out.println(matcher.lookingAt()); // true

final var matchResult = matcher.toMatchResult();

System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 3
System.out.println(matchResult.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc 345");

final var stream = matcher.results();

stream.forEach(matchResult -> {

    System.out.println("------");
    System.out.println("start : " + matchResult.start());
    System.out.println("end : " + matchResult.end());
    System.out.println("group : " + matchResult.group());
});

// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 14
//group : aaaabc

int end (int group)

Devuelve el desplazamiento después del último carácter de la subsecuencia capturada por el grupo dado durante esta coincidencia. (Traducción automática)

final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var matchResult = matcher.toMatchResult();

System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

final var groupCount = matchResult.groupCount();
System.out.println(groupCount); // 2

for (int i = 1; i <= groupCount; i++) {

    System.out.println("------");
    System.out.println("i : " + i);
    System.out.println("start : " + matchResult.start(i));
    System.out.println("end : " + matchResult.end(i));
    System.out.println("group : " + matchResult.group(i));
}

// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz

default int end (String name)

Devuelve el desplazamiento después del último carácter de la subsecuencia capturada por el grupo de captura nombrado dado durante la operación de coincidencia anterior. (Traducción automática)

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

{
    final var name = "A";

    System.out.println(matchResult.start(name)); // 0
    System.out.println(matchResult.end(name)); // 3
    System.out.println(matchResult.group(name)); // "abc"
}

{
    final var name = "B";

    System.out.println(matchResult.start(name)); // 8
    System.out.println(matchResult.end(name)); // 11
    System.out.println(matchResult.group(name)); // "xyz"
}

String group ()

Devuelve la subsecuencia de entrada que coincide con la coincidencia anterior. (Traducción automática)

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc");

System.out.println(matcher.lookingAt()); // true

final var matchResult = matcher.toMatchResult();

System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 3
System.out.println(matchResult.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc 345");

final var stream = matcher.results();

stream.forEach(matchResult -> {

    System.out.println("------");
    System.out.println("start : " + matchResult.start());
    System.out.println("end : " + matchResult.end());
    System.out.println("group : " + matchResult.group());
});

// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 14
//group : aaaabc

String group (int group)

Devuelve la subsecuencia de entrada capturada por el grupo dado durante la operación de coincidencia anterior. (Traducción automática)

final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var matchResult = matcher.toMatchResult();

System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

final var groupCount = matchResult.groupCount();
System.out.println(groupCount); // 2

for (int i = 1; i <= groupCount; i++) {

    System.out.println("------");
    System.out.println("i : " + i);
    System.out.println("start : " + matchResult.start(i));
    System.out.println("end : " + matchResult.end(i));
    System.out.println("group : " + matchResult.group(i));
}

// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz

default String group (String name)

Devuelve la subsecuencia de entrada capturada por el grupo de captura nombrado dado durante la operación de coincidencia anterior. (Traducción automática)

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

{
    final var name = "A";

    System.out.println(matchResult.start(name)); // 0
    System.out.println(matchResult.end(name)); // 3
    System.out.println(matchResult.group(name)); // "abc"
}

{
    final var name = "B";

    System.out.println(matchResult.start(name)); // 8
    System.out.println(matchResult.end(name)); // 11
    System.out.println(matchResult.group(name)); // "xyz"
}

int groupCount ()

Devuelve el número de grupos de captura en el patrón de resultado de esta coincidencia. (Traducción automática)

final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var matchResult = matcher.toMatchResult();

System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

final var groupCount = matchResult.groupCount();
System.out.println(groupCount); // 2

for (int i = 1; i <= groupCount; i++) {

    System.out.println("------");
    System.out.println("i : " + i);
    System.out.println("start : " + matchResult.start(i));
    System.out.println("end : " + matchResult.end(i));
    System.out.println("group : " + matchResult.group(i));
}

// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz

default boolean hasMatch ()

Devuelve si contiene una coincidencia válida de una operación de búsqueda o coincidencia anterior. (Traducción automática)

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc");

{
    System.out.println(matcher.find()); // true

    final var matchResult = matcher.toMatchResult();
    System.out.println(matchResult.hasMatch()); // true
    System.out.println(matchResult.start()); // 0
    System.out.println(matchResult.end()); // 3
    System.out.println(matchResult.group()); // "abc"
}

{
    System.out.println(matcher.find()); // true

    final var matchResult = matcher.toMatchResult();
    System.out.println(matchResult.hasMatch()); // true
    System.out.println(matchResult.start()); // 8
    System.out.println(matchResult.end()); // 13
    System.out.println(matchResult.group()); // "aaabc"
}

{
    System.out.println(matcher.find()); // false

    final var matchResult = matcher.toMatchResult();
    System.out.println(matchResult.hasMatch()); // false
}

default Map<String,Integer> namedGroups ()

Devuelve un mapa no modificable desde la captura de nombres de grupos hasta números de grupos. (Traducción automática)

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true
final var matchResult = matcher.toMatchResult();

System.out.println("-- groups --");
for (final var entry : matchResult.namedGroups().entrySet()) {
    System.out.println("entry : " + entry);

    final var num = entry.getValue();
    System.out.println("  group : " + matchResult.group(num));
}

// Result
// ↓
//true
//-- groups --
//entry : A=1
//  group : abc
//entry : B=2
//  group : xyz

int start ()

Devuelve el índice de inicio del partido. (Traducción automática)

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc");

System.out.println(matcher.lookingAt()); // true

final var matchResult = matcher.toMatchResult();

System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 3
System.out.println(matchResult.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc 345");

final var stream = matcher.results();

stream.forEach(matchResult -> {

    System.out.println("------");
    System.out.println("start : " + matchResult.start());
    System.out.println("end : " + matchResult.end());
    System.out.println("group : " + matchResult.group());
});

// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 14
//group : aaaabc

int start (int group)

Devuelve el índice de inicio de la subsecuencia capturada por el grupo dado durante esta coincidencia. (Traducción automática)

final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var matchResult = matcher.toMatchResult();

System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

final var groupCount = matchResult.groupCount();
System.out.println(groupCount); // 2

for (int i = 1; i <= groupCount; i++) {

    System.out.println("------");
    System.out.println("i : " + i);
    System.out.println("start : " + matchResult.start(i));
    System.out.println("end : " + matchResult.end(i));
    System.out.println("group : " + matchResult.group(i));
}

// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz

default int start (String name)

Devuelve el índice de inicio de la subsecuencia capturada por el grupo de captura nombrado dado durante la operación de coincidencia anterior. (Traducción automática)

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

{
    final var name = "A";

    System.out.println(matchResult.start(name)); // 0
    System.out.println(matchResult.end(name)); // 3
    System.out.println(matchResult.group(name)); // "abc"
}

{
    final var name = "B";

    System.out.println(matchResult.start(name)); // 8
    System.out.println(matchResult.end(name)); // 11
    System.out.println(matchResult.group(name)); // "xyz"
}

Related posts

To top of page