1
\$\begingroup\$

This program takes blank spaces before some chars like > < />, and if there is more than one blank space in the line, it will reduce to one.

return source.replaceAll("\\s{2,}", " ").replaceAll("[[\\>][\\s]][[\\s][\\>]]", ">").replaceAll("[[\\<][\\s]][[\\s][\\<]]", "<").replaceAll("[\\s][/>]", "/>");

How can I improve my regex code?

My test code is:

<?xml version="1.0" encoding="UTF-8"?>
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe"  versao="2.00" > 
<NFe xmlns="http://www.portalfiscal.inf.br/nfe"><infNFe Id="NFe31130821483359001109550010002217051649842350" versao="2.00"><ide><cUF>31</cUF><cNF>64984235</cNF><natOp>VENDA PROD.ESTAB.SUBST.TRIBUTA</natOp><indPag>1</indPag><mod>55</mod><serie>1</serie><nNF>221705</nNF><dEmi>2013-08-07</dEmi><dSaiEnt>2013-08-07</dSaiEnt><tpNF>1</tpNF><cMunFG>3147105</cMunFG><tpImp>2</tpImp><tpEmis>1</tpEmis><cDV>0</cDV><tpAmb>1</tpAmb><finNFe>1</finNFe><procEmi>0</procEmi><verProc>4.0.0.36</verProc></ide><emit><CNPJ>21483359001109</CNPJ><xNome>COOPERATIVA DOS GRANJEIROS DO OESTE DE MINAS LTDA</xNome><xFant>ABATEDOURO</xFant><enderEmit><xLgr>Rod RODOVIA BR 262</xLgr><nro>S/N</nro><xCpl>KM - 402</xCpl><xBairro>POVOADO GOMES</xBairro><cMun>3147105</cMun><xMun>PARA DE MINAS</xMun><UF>MG</UF><CEP>35661470</CEP><cPais>1058</cPais><xPais>Brasil</xPais><fone>03732365566</fone></enderEmit><IE>4713164920659</IE><CRT>3</CRT></emit><dest><CNPJ>07121048000105</CNPJ><xNome>SUPERMERCADO SAFRA LTDA</xNome><enderDest><xLgr>RUA TEOFILO ANDRADE</xLgr><nro>7</nro><xBairro>CENTRO</xBairro><cMun>3121407</cMun><xMun>DESTERRO DE ENTRE RIOS</xMun><UF>MG</UF><CEP>35494000</CEP><cPais>1058</cPais><xPais>Brasil</xPais><fone>3137361535</fone></enderDest><IE>2142099300090</IE></dest><det nItem="1"><prod><cProd>11007</cProd><cEAN>7898296060227</cEAN><xProd>COXA S/ COXA FRANGO CONGELADO CX. 20KG</xProd><NCM>02071400</NCM><EXTIPI>00</EXTIPI><CFOP>5401</CFOP><uCom>KG</uCom><qCom>100</qCom><vUnCom>3.7</vUnCom><vProd>370.00</vProd><cEANTrib>7898296060227</cEANTrib><uTrib>KG</uTrib><qTrib>100</qTrib><vUnTrib>3.7</vUnTrib><indTot>1</indTot><xPed>103197</xPed><nItemPed>3</nItemPed></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>3</modBC><pRedBC>61.11</pRedBC><vBC>143.89</vBC><pICMS>18.00</pICMS><vICMS>25.90</vICMS><modBCST>4</modBCST><pMVAST>15.00</pMVAST><pRedBCST>0.39</pRedBCST><vBCST>165.48</vBCST><pICMSST>18.00</pICMSST><vICMSST>3.89</vICMSST></ICMS70></ICMS><IPI><cEnq>999</cEnq><IPINT><CST>53</CST></IPINT></IPI><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto><infAdProd>7898296060227 -</infAdProd></det><det nItem="2"><prod><cProd>11046</cProd><cEAN>7898296060173</cEAN><xProd>FRANGO CONGELADO CX. 20KG</xProd><NCM>02071200</NCM><EXTIPI>00</EXTIPI><CFOP>5401</CFOP><uCom>KG</uCom><qCom>200</qCom><vUnCom>3</vUnCom><vProd>600.00</vProd><cEANTrib>7898296060173</cEANTrib><uTrib>KG</uTrib><qTrib>200</qTrib><vUnTrib>3</vUnTrib><indTot>1</indTot><xPed>103197</xPed><nItemPed>1</nItemPed></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>3</modBC><pRedBC>61.11</pRedBC><vBC>233.34</vBC><pICMS>18.00</pICMS><vICMS>42.00</vICMS><modBCST>4</modBCST><pMVAST>15.00</pMVAST><pRedBCST>0.39</pRedBCST><vBCST>268.34</vBCST><pICMSST>18.00</pICMSST><vICMSST>6.30</vICMSST></ICMS70></ICMS><IPI><cEnq>999</cEnq><IPINT><CST>53</CST></IPINT></IPI><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto><infAdProd>7898296060173 -</infAdProd></det><det nItem="3"><prod><cProd>11004</cProd><cEAN>7898296060203</cEAN><xProd>PEITO FRANGO CONGELADO CX. 20 KG</xProd><NCM>02071400</NCM><EXTIPI>00</EXTIPI><CFOP>5401</CFOP><uCom>KG</uCom><qCom>100</qCom><vUnCom>3.9</vUnCom><vProd>390.00</vProd><cEANTrib>7898296060203</cEANTrib><uTrib>KG</uTrib><qTrib>100</qTrib><vUnTrib>3.9</vUnTrib><indTot>1</indTot><xPed>103197</xPed><nItemPed>4</nItemPed></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>3</modBC><pRedBC>61.11</pRedBC><vBC>151.67</vBC><pICMS>18.00</pICMS><vICMS>27.30</vICMS><modBCST>4</modBCST><pMVAST>15.00</pMVAST><pRedBCST>0.39</pRedBCST><vBCST>174.42</vBCST><pICMSST>18.00</pICMSST><vICMSST>4.10</vICMSST></ICMS70></ICMS><IPI><cEnq>999</cEnq><IPINT><CST>53</CST></IPINT></IPI><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto><infAdProd>7898296060203 -</infAdProd></det><total><ICMSTot><vBC>528.90</vBC><vICMS>95.20</vICMS><vBCST>608.24</vBCST><vST>14.29</vST><vProd>1360.00</vProd><vFrete>0.00</vFrete><vSeg>0.00</vSeg><vDesc>0.00</vDesc><vII>0.00</vII><vIPI>0.00</vIPI><vPIS>0.00</vPIS><vCOFINS>0.00</vCOFINS><vOutro>0.00</vOutro><vNF>1374.29</vNF></ICMSTot></total><transp><modFrete>0</modFrete><transporta><CNPJ>21483359001109</CNPJ><xNome>COOPERATIVA DOS GRANJEIROS DO OESTE DE MINAS LTDA ABATEDOURO</xNome><IE>4713164920659</IE><xEnder>Rod RODOVIA BR 262 N. S/N KM - 402</xEnder><xMun>PARA DE MINAS</xMun><UF>MG</UF></transporta><veicTransp><placa>HIH2860</placa><UF>MG</UF></veicTransp><vol><qVol>500</qVol><esp>VOLUMES</esp><pesoL>400.000</pesoL><pesoB>400.000</pesoB></vol></transp><cobr><fat><nFat>221705</nFat><vOrig>1374.29</vOrig><vLiq>1374.29</vLiq></fat><dup><nDup>000221705-01</nDup><dVenc>2013-08-28</dVenc><vDup>1374.29</vDup></dup></cobr><infAdic><infAdFisco>REDUCAO DE 61,11% CONF LETRA A1 ITEM 19 DA - PARTE 1 DO ANEXO IV RICMS 43.080/02 - E C/ COBRANCA POR ST - -</infAdFisco><infCpl>ORDEM DE CARGA: 18313</infCpl></infAdic></infNFe><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI="#NFe31130821483359001109550010002217051649842350"><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /><Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /><DigestValue>JOjHIJzegFkL7ZfGkfAgSwP0+s4=</DigestValue></Reference></SignedInfo><SignatureValue>Qvsom1D+sQZdrq9Y5rfMCEDBUPeRD5iToj3prhoUh0Mfy1zR0dLzTIyp56qIGsPXYogheRuR9PQVzqEc3AK5alK3tkEf+T+zYz3F/xAb7Fyo+/txye625R0D4zm23xvINlLcg0Jhs4S+OIwvDjkX6ICD/bQbP0U55mhqSW71hAYzHkRWiitmy5MJQ00YphvPBw2ikaHrFEy8cy/gYxzs3HWOHHk51AavnhQ/eqK2VcJP7NTAXeECJniz21tVE3CwzLLCRqJhm8nNAsVt8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/pCHijYtKQ68o5LxAZ1jswQlrHMEyTbfLi7Y2iN/C7cR2KDg/TG7CxzNrVgJvw+vMHngaNUfqHzBUz++kLtynjXGxiSOg/4RfTP82zkfqdT1sisVNw3n1ZyZpOGAkWAyv8kjsBPKUZlL+lfDYrf9clr9JUSA4UI91cxYMQubFAPvXBKe18OPGZ1P2SINJpwiH/K2MHb7OIydln4Y+vfmoVq/lVwFbmqZNCGRbncv5ZU/qhybmeHyL1P0af1edDmV2kWB6CNDVaUE4KI+Zm3TJwYvC3aasYULvgb+cG1qvh5i+D/VzN+x/ue2x/8KMMGgEhK/OqojES3RGy9ALjz+1QOQbkIo8Yv3/Ram0HcMBRGMdjn68EubyZCPw13ipCjeSTxhLokrFsSiaMjN805wHLoa7IS7KZhR7IjM8RmRP8/SV3lqE4xi8LWe718Ef6olLwPAYWMLD/dPqtCNXdQvZ6M0a0mWfjLx1bbMIbetp9J4pdcr4aAON2lL194ilX6q4g93RktJJAsygJHo7GwiNf89IpuOMz5AT5tDGAV7V4+J2ObSCzIVz8QJClKTuc9+xcbtCHI37T7XREcG+JAOQaBJYvMng==</X509Certificate></X509Data></KeyInfo></Signature></NFe>
<protNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.00"><infProt><tpAmb>1</tpAmb><verAplic>13_0_94</verAplic><chNFe>31130821483359001109550010002217051649842350</chNFe><dhRecbto>2013-08-07T01:52:47</dhRecbto><nProt>131131174998806</nProt><digVal>JOjHIJzegFkL7ZfGkfAgSwP0+s4=</digVal><cStat>100</cStat><xMotivo>Autorizado o uso da NF-e</xMotivo></infProt></protNFe>
</nfeProc>
\$\endgroup\$
4
  • 7
    \$\begingroup\$ Why do you need this? If you have a decent XML-parser then does it really matter how many spaces there are before >-characters? Because you DO use a XML-parser to parse your XML data, don't you? \$\endgroup\$ Commented Nov 12, 2013 at 0:16
  • \$\begingroup\$ because some documents have \n and I want to take it off. \$\endgroup\$ Commented Nov 12, 2013 at 11:50
  • 2
    \$\begingroup\$ Why? What harm has \n ever done to you? \$\endgroup\$ Commented Nov 12, 2013 at 12:33
  • \$\begingroup\$ can brake the string sequence \$\endgroup\$ Commented Nov 12, 2013 at 13:44

1 Answer 1

4
\$\begingroup\$

This will suffice:

source.replaceAll("\\s+([<> ]|/>)", "$1");

Regular expression visualization

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I know this is pretty simple but would you please try to explain it a little bit? \$\endgroup\$
    – Malachi
    Commented Nov 12, 2013 at 13:48
  • \$\begingroup\$ \O/ so fine, could you suggest to me some reading to know about regex? Because I want to know about $1 and how do you generate this image? \$\endgroup\$ Commented Nov 12, 2013 at 13:50
  • \$\begingroup\$ @DiegoMacario For more reading about regex, see regular-expressions.info \$\endgroup\$ Commented Nov 12, 2013 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.