String.replaceAll()不起作用 [英] String.replaceAll() is not working
本文介绍了String.replaceAll()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编辑一些来自tesseract ocr的电子邮件。
I am editing some email that got from 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中连续赋值不起作用?
But when I assigned the result to a new String after every replacement, it works fine. Why continuous assignment in the same String is not working?
推荐答案
你会在字符串的Javadoc。 replaceAll()第一个参数是正则表达式。
句号(。
)与管道有特殊含义( |
)大括号(}
)。您需要将它们全部转义,例如:
A period (.
) has a special meaning there as does a pipe (|
) as does a curly brace (}
). You need to escape them all, such as:
email = email.replaceAll("gmaii\\.com", "gmail.com");
这篇关于String.replaceAll()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文