String.replaceAll()对某些字符串不起作用 [英] String.replaceAll() is not working for some strings

查看:52
本文介绍了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时,它都可以正常工作.为什么在同一字符串中进行连续分配不起作用?

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?

推荐答案

您会在

You'll note in the Javadoc for String.replaceAll() that the first argument is a regular expression.

句点(.)在这里有特殊的含义,在管道( | )和花括号(} )中也有特殊含义.您需要将它们全部转义,例如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆