我正在编辑一些来自tesseract ocr的电子邮件。
这是我的代码:
if (email != null) {
email = email.replaceAll(" ", "");
email = email.replaceAll("caneer", "career");
email = email.replaceAll("canaer", "career");
email = email.replaceAll("canear", "career");
email = email.replaceAll("caraer", "career");
email = email.replaceAll("carear", "career");
email = email.replace("|", "l");
email = email.replaceAll("}", "j");
email = email.replaceAll("j3b", "job");
email = email.replaceAll("gmaii.com", "gmail.com");
email = email.replaceAll("hotmaii.com", "hotmail.com");
email = email.replaceAll(".c0m", ".com");
email = email.replaceAll(".coin", ".com");
email = email.replaceAll("consuit", "consult");
}
return email;
但输出不正确。
输入:
amrut=ac.hrworks@g mai|.com
输出
lalcl.lhlrlwlolrlklsl@lglmlalil|l.lclolml
但是当我在每次替换后将结果分配给新的String时,它工作正常。为什么在同一个String中连续赋值不起作用?
答案 0 :(得分:38)
您会在Javadoc for String.replaceAll()中注意到第一个参数是regular expression。
句点(.
)与管道(|
)一样具有特殊含义,大括号(}
)也是如此。你需要将它们全部转义,例如:
email = email.replaceAll("gmaii\\.com", "gmail.com");
答案 1 :(得分:10)
(这是Java吗?)
请注意,在Java中,replaceAll接受正则表达式,并且点匹配任何字符。你需要逃避点或使用
somestring.replaceAll(Pattern.quote("gmail.com"), "replacement");
还要注意这里的拼写错误:
email = emai.replaceAll("canear", "career");
应该是
email = email.replaceAll("canear", "career");
答案 2 :(得分:6)
您必须按.
之类的\\.
转义,如下所示:
if (email != null) {
email = email.replaceAll(" ", "");
email = email.replaceAll("caneer", "career");
email = email.replaceAll("canaer", "career");
email = email.replaceAll("canear", "career");
email = email.replaceAll("caraer", "career");
email = email.replaceAll("carear", "career");
email = email.replace("|", "l");
email = email.replaceAll("}", "j");
email = email.replaceAll("j3b", "job");
email = email.replaceAll("gmaii\\.com", "gmail.com");
email = email.replaceAll("hotmaii\\.com", "hotmail.com");
email = email.replaceAll("\\.c0m", "com");
email = email.replaceAll("\\.coin", "com");
email = email.replaceAll("consuit", "consult");
}
return email;
答案 3 :(得分:6)
通过认识到replaceAll()
第一个参数是regex
,你可以使你的比较更少
例如,您可以通过以下career
regex
的拼写错误拼写
email = email.replaceAll("ca[n|r][e|a][e|a]r", "career"));
答案 4 :(得分:5)
您正在使用一些正则表达式字符。
请使用\
或使用Pattern.quote
方法
答案 5 :(得分:5)
我认为你不知道replaceAll
的第一个参数是正则表达式。
.
,|
,}
可能会以与您的期望不同的方式进行解释。
. Any character (may or may not match line terminators)
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
对于空间,你最好使用
\s A whitespace character: [ \t\n\x0B\f\r]
并使用前导\\