Skip to content

Instantly share code, notes, and snippets.

@fmquaglia
Created December 11, 2012 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmquaglia/4258259 to your computer and use it in GitHub Desktop.
Save fmquaglia/4258259 to your computer and use it in GitHub Desktop.
Ejemplo de cómo usar Regex para encontrar los últimos dígitos en un string
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* User: fabricio
* Date: 12/11/12
* Time: 9:22 AM
*/
public class Main {
public static void main(String[] args) {
String testString = "wkceekbc543453bewkgfb3455##345##cj4534fh3455cfh43554354"; // el string sobre el cual se ejecuta la prueba, termina en numeros pero se puede probar que termine en letras, entonces no debería devolver nada
Pattern regex = Pattern.compile("\\d+$"); // la expresión regular
Matcher buscadorUltimosNumeros = regex.matcher(testString); // se ejecuta la expresión regular sobre el string de prueba usando la clase matcher
while(buscadorUltimosNumeros.find()){ // find() devuelve true por cada ocurrencia
System.out.println(testString.substring(buscadorUltimosNumeros.start(),buscadorUltimosNumeros.end())); // start() devuelve el índice del caracter dónde empieza la ocurrencia actual, y end() dónde termina
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment