Skip to content

Instantly share code, notes, and snippets.

@jonatasemidio
Created January 29, 2016 16:45
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 jonatasemidio/8898e8b4290cf059dff6 to your computer and use it in GitHub Desktop.
Save jonatasemidio/8898e8b4290cf059dff6 to your computer and use it in GitHub Desktop.
REGEX = "^[\\w\\s/<>?\".=-]*[áéíóúçàãõâêô:\\s]*\$"
nome = " jonatas#!@#%¨&* emidio"
texto = '''
<?xml¨&*()¨&*( version="1.0" encoding="ISO-8859-1"?>
<SAIDA>
<HEADER>
<ERRO>
<CODIGO-ERRO>1454014207309</CODIGO-ERRO>%¨&*()
<ORIGEM-ERRO>The data "ˆ" is not legal for a JDOM character content 0x0 is not a legal XML character.</ORIGEM-ERRO>
</ERRO>
</HEADER>
</SAIDA>
'''
xmlvalido0 = texto.collect { linha -> linha.collect{ if(it.matches(REGEX)) it else '' }.join() }.join()
println xmlvalido0
xmlvalido1 = texto.collect { linha ->
linha.collect{
if(it.matches(REGEX))
it
else
''
}.join()
}.join()
println xmlvalido1
StringBuilder xmlvalido2 = new StringBuilder();
for (String linha in texto.split("\n")){
String[] caracteres = linha.split("");
StringBuilder linhaValida = new StringBuilder();
for (int i = 0; i < caracteres.length; i++){
if(caracteres[i].matches(REGEX)){
linhaValida.append(caracteres[i]);
}
}
xmlvalido2.append(linhaValida + "\n");
}
println xmlvalido2
//def parseline(line){
// sb = new StringBuffer()
// line.collect{ if(it.matches(REGEX)) { sb << it } }
// return sb.toString()
//}
//sb = new StringBuffer()
//nome.collect{ if(it.matches(REGEX)) { sb << it } }
//println sb.toString()
//println nome.collect{ if(it.matches(REGEX)) it else '' }.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment