gpt4 book ai didi

java - 有没有办法改进这个正则表达式代码?

转载 作者:行者123 更新时间:2023-12-01 13:52:02 27 4
gpt4 key购买 nike

我用 Java 编写了这段代码,以便在一些字符(如 >)之前保留空格。 < />如果该行中有多个空格,则会减少到一个。

所以我的代码是。

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

那么我可以改进我的正则表达式代码吗?

我的测试代码是..

<?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/bQbP0U55mhqSW71h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SqGSIb3DQEBCwUAA4ICAQCv4rUWP0AkysbiVpsrnJFI3Z7TPeKsNeuAIQgTtV9HY7Mq3wmorcZmvidxb1spBp7/7HJuXUO2FmWO4/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>

最佳答案

如何将 "\\s+" 与替换函数一起使用:string.replaceAll("\\s+", "") 这将删除所有空白,但一个。

X+ 表达式称为量词:指定 X 出现一次或多次

编辑:回复您的评论:

最简单的方法是连续两次调用 replaceAll("\\s+>", ">"),然后调用 replaceAll("\\s+(/>)",“/>”)<​​

String a = "asd     > bhsj       <   />   b";
a = a.replaceAll("\\s+>", " >").replaceAll("\\s+(/>)", " />");
System.out.println(a);

关于java - 有没有办法改进这个正则表达式代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19890037/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com