从字符串中删除控件,隐藏的不需要的字符

时间:2018-11-26 21:17:34

标签: java regex string replaceall

请在我被锤击之前,因为还有其他像我这样的stackoverflow问题,但它们不起作用。我试图从入站邮件中删除不需要的字符,但没有成功。我不知道这些字符是什么或代表什么,但是,它们似乎像回车符,换行符或换行符一样破坏了数据。 除了结尾处,我需要保留所有空格。 我看到的字符是^ M和^ C。有时一起使用,并且单独使用。

我的测试代码基本上来自观察其他类似问题。

String msg = exchange.getIn().getBody(String.class);

log.info("Message before apply filter: " + msg);
filteredMessage = msg.replaceAll("[^\\x00-\\x7F]","");
log.info("Remove non-ASCII characters: " + filteredMessage);
filteredMessage = msg.replaceAll("[\\p{C}]","");
log.info("Remove all Control characters: " + filteredMessage);
filteredMessage = msg.replaceAll("[\\p{Cntrl}\\p{Cc}\\p{Cf}\\p{Co}\\p{Cn}]","");
log.info("Remove some Control characters: " + filteredMessage);
filteredMessage = msg.replaceAll("[^\\p{Print}]","");
log.info("Remove non printable characters: " + filteredMessage);
filteredMessage = msg.trim();
log.info("Trim: " + filteredMessage);
filteredMessage = msg.replaceAll("\\cM","");
log.info("Remove ^M Control characters: " + filteredMessage);
filteredMessage = msg.replaceAll("^M","");
log.info("Remove ^M Control characters: " + filteredMessage);

exchange.getIn().setBody(filteredMessage);

样本数据文件:

A   291511191831421742XXXXXXXXXXWRN/WN18111917420077000009ENG 2 IGN B FAULT^M ^C

A   056611191832641742XXXXXXXXXXFLR/FR18111917410032470002BRK TEMP SENSOR4(6GW)/  BTMU(2GW)^M/IDBSCU 1^M  ^C

A   080011191830061749XXXXXXXXXXMPF/AN.N306DN/FIDAL800    /DM181119142800/DAKSMF/DSKMSP/WN18111916310034000006NAV ATC/XPDR 1 FAULT^M,18111917480032000009BRAKES HOT^M/FR18111916310034523306ATC 1(1SH1)^M/IDATC 1^M/FR18111916310034723406ATC1(1SH1)/TCAS(1000SG)^M/IDTCAS^M/FR18111917120022833406AFS:FMGC2^M/IDAFS 1^M,IR 1^M,IR 2^M,IR 3^M/FR18111917120022833406AFS:FMGC1^M/IDAFS 1^M,IR 1^M,IR 2^M,IR 3^M ^C

我的过滤器不起作用。这是结果。就像正则表达式根本不起作用,或者我在做一些愚蠢的事情。谢谢大家!

 Message before apply filter: A   056611191832641742XXXXXXXXXXFLR/FR18111917410032470002BRK TEMP SENSOR4(6GW)/  BTMU(2GW)^M/IDBSCU 1^M  ^C

Remove non-ASCII characters: A   056611191832641742XXXXXXXXXXFLR/FR18111917410032470002BRK TEMP SENSOR4(6GW)/  BTMU(2GW)^M/IDBSCU 1^M  ^C

Remove all Control characters: A   056611191832641742XXXXXXXXXXFLR/FR18111917410032470002BRK TEMP SENSOR4(6GW)/  BTMU(2GW)^M/IDBSCU 1^M  ^C

Remove some Control characters: A   056611191832641742XXXXXXXXXXFLR/FR18111917410032470002BRK TEMP SENSOR4(6GW)/  BTMU(2GW)^M/IDBSCU 1^M  ^C

Remove non printable characters: A   056611191832641742XXXXXXXXXXFLR/FR18111917410032470002BRK TEMP SENSOR4(6GW)/  BTMU(2GW)^M/IDBSCU 1^M  ^C

Trim: A   056611191832641742XXXXXXXXXXFLR/FR18111917410032470002BRK TEMP SENSOR4(6GW)/  BTMU(2GW)^M/IDBSCU 1^M  ^C

Remove ^M Control characters: A   056611191832641742XXXXXXXXXXFLR/FR18111917410032470002BRK TEMP SENSOR4(6GW)/  BTMU(2GW)^M/IDBSCU 1^M  ^C

Remove ^M Control characters: A   056611191832641742XXXXXXXXXXFLR/FR18111917410032470002BRK TEMP SENSOR4(6GW)/  BTMU(2GW)^M/IDBSCU 1^M  ^C

1 个答案:

答案 0 :(得分:1)

尝试以下方法,看看其进展如何:

String msg = exchange.getIn().getBody(String.class);

log.info("Message before apply filter: " + msg);
filteredMessage = msg.replaceAll("[^\\x00-\\x7F]","");
log.info("Remove non-ASCII characters: " + filteredMessage);
filteredMessage =filteredMessage.replaceAll("[\\p{C}]","");
log.info("Remove all Control characters: " + filteredMessage);
filteredMessage = filteredMessage.replaceAll("[\\p{Cntrl}\\p{Cc}\\p{Cf}\\p{Co}\\p{Cn}]","");
log.info("Remove some Control characters: " + filteredMessage);
filteredMessage = filteredMessage.replaceAll("[^\\p{Print}]","");
log.info("Remove non printable characters: " + filteredMessage);
filteredMessage = filteredMessage.trim();
log.info("Trim: " + filteredMessage);
filteredMessage = filteredMessage.replaceAll("\\cM","");
log.info("Remove ^M Control characters: " + filteredMessage);
filteredMessage = filteredMessage.replaceAll("^M","");
log.info("Remove ^M Control characters: " + filteredMessage);

此后,如果仍然看到不需要的字符,则需要使用转义字符\来转义反斜杠和其他元字符:

<>()[]{}\^-=$!|?*+.

因此,您需要例如:.replaceALl("\^M","");