import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
class TesteRegex {
private static final Pattern AGENCIA_CONTA = Pattern.compile (
"(?:Agência: [0-9]{4} Conta: [0-9]{5}-[0-9X])|" +
"(?:Conta: [0-9]{5}-[0-9X] Agência: [0-9]{4})" ) ;
public static void main
( String [ ] args
) { + "Banana abacaxi pêra Agência: 5720 Conta: 43821-X abacate "
+ "melancia Agência: 3481 Conta: 53895-0. verde azul "
+ "amarelo Agência: 6666 Conta: 66667-NÃO É ESSA "
+ "Agência: 9123 Conta: 44578-2 "
+ "laranja Conta: 43210-7 Agência: 6589 verde "
+ "rosa lilás Conta: 77777-7 Não vai dar Agência: 4444" ;
Matcher m = AGENCIA_CONTA.matcher ( texto) ;
while ( m.find ( ) ) {
String achou
= texto.
substring ( m.
start ( ) , m.
end ( ) ) ; System .
out .
println ( "Achou nas posições " + m.
start ( ) + "-" + m.
end ( ) + ": " + achou) ;
if ( achou.startsWith ( "Agência:" ) ) {
agencia = achou.substring ( 9 , 13 ) ;
conta = achou.substring ( 21 , 28 ) ;
} else {
agencia = achou.substring ( 24 , 28 ) ;
conta = achou.substring ( 7 , 14 ) ;
}
System .
out .
println ( "Os valores encontrados são: " + agencia
+ " e " + conta
+ "." ) ; }
}
}
aW1wb3J0IGphdmEudXRpbC5yZWdleC5NYXRjaGVyOwppbXBvcnQgamF2YS51dGlsLnJlZ2V4LlBhdHRlcm47CgpjbGFzcyBUZXN0ZVJlZ2V4IHsKCiAgICBwcml2YXRlIHN0YXRpYyBmaW5hbCBQYXR0ZXJuIEFHRU5DSUFfQ09OVEEgPSBQYXR0ZXJuLmNvbXBpbGUoCiAgICAJICAgICIoPzpBZ8OqbmNpYTogWzAtOV17NH0gQ29udGE6IFswLTldezV9LVswLTlYXSl8IiArCiAgICAJICAgICIoPzpDb250YTogWzAtOV17NX0tWzAtOVhdIEFnw6puY2lhOiBbMC05XXs0fSkiKTsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgU3RyaW5nIHRleHRvID0gIiIKICAgICAgICAgICAgICAgICsgIkJhbmFuYSBhYmFjYXhpIHDDqnJhIEFnw6puY2lhOiA1NzIwIENvbnRhOiA0MzgyMS1YIGFiYWNhdGUgIgogICAgICAgICAgICAgICAgKyAibWVsYW5jaWEgQWfDqm5jaWE6IDM0ODEgQ29udGE6IDUzODk1LTAuIHZlcmRlIGF6dWwgIgogICAgICAgICAgICAgICAgKyAiYW1hcmVsbyBBZ8OqbmNpYTogNjY2NiBDb250YTogNjY2NjctTsODTyDDiSBFU1NBICIKICAgICAgICAgICAgICAgICsgIkFnw6puY2lhOiA5MTIzIENvbnRhOiA0NDU3OC0yICIKICAgICAgICAgICAgICAgICsgImxhcmFuamEgQ29udGE6IDQzMjEwLTcgQWfDqm5jaWE6IDY1ODkgdmVyZGUgIgogICAgICAgICAgICAgICAgKyAicm9zYSBsaWzDoXMgQ29udGE6IDc3Nzc3LTcgTsOjbyB2YWkgZGFyIEFnw6puY2lhOiA0NDQ0IjsKCiAgICAgICAgTWF0Y2hlciBtID0gQUdFTkNJQV9DT05UQS5tYXRjaGVyKHRleHRvKTsKICAgICAgICB3aGlsZSAobS5maW5kKCkpIHsKICAgICAgICAJU3RyaW5nIGFjaG91ID0gdGV4dG8uc3Vic3RyaW5nKG0uc3RhcnQoKSwgbS5lbmQoKSk7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiQWNob3UgbmFzIHBvc2nDp8O1ZXMgIiArIG0uc3RhcnQoKSArICItIiArIG0uZW5kKCkgKyAiOiAiCiAgICAgICAgICAgICAgICAgICAgKyBhY2hvdSk7CiAgICAgICAgICAgIFN0cmluZyBhZ2VuY2lhLCBjb250YTsKICAgICAgICAgICAgaWYgKGFjaG91LnN0YXJ0c1dpdGgoIkFnw6puY2lhOiIpKSB7CiAgICAgICAgICAgICAgICBhZ2VuY2lhID0gYWNob3Uuc3Vic3RyaW5nKDksIDEzKTsKICAgICAgICAgICAgICAgIGNvbnRhID0gYWNob3Uuc3Vic3RyaW5nKDIxLCAyOCk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBhZ2VuY2lhID0gYWNob3Uuc3Vic3RyaW5nKDI0LCAyOCk7CiAgICAgICAgICAgICAgICBjb250YSA9IGFjaG91LnN1YnN0cmluZyg3LCAxNCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJPcyB2YWxvcmVzIGVuY29udHJhZG9zIHPDo286ICIgKyBhZ2VuY2lhICsgIiBlICIgKyBjb250YSArICIuIik7CiAgICAgICAgfQogICAgfQp9