String.replaceAll() is not working

后端 未结 6 1610
野趣味
野趣味 2020-12-03 13:45

I am editing some email that got from tesseract ocr.

Here is my code:

 if (email != null) {
        email = email.replaceAll(\" \",         


        
相关标签:
6条回答
  • 2020-12-03 14:22

    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");
    
    0 讨论(0)
  • 2020-12-03 14:27

    I think you are not aware that first parameter of replaceAll is regex.

    . , |, } might be interpreted in a different way from your expectation.

    .   Any character (may or may not match line terminators)
    

    http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

    For space you better use

    \s  A whitespace character: [ \t\n\x0B\f\r]
    

    and escape other special characters with a leading \\

    0 讨论(0)
  • 2020-12-03 14:28

    (Is this Java?)

    Note that in Java, replaceAll accepts a regular expression and the dot matches any character. You need to escape the dot or use

    somestring.replaceAll(Pattern.quote("gmail.com"), "replacement");
    

    Also note the typo here:

    email = emai.replaceAll("canear", "career");
    

    should be

    email = email.replaceAll("canear", "career");
    
    0 讨论(0)
  • 2020-12-03 14:30

    By realizing that replaceAll() first argument is regex you can make your comparisons much less

    For example you can check for possible misspellings of the word career by the following regex

    email = email.replaceAll("ca[n|r][e|a][e|a]r", "career"));

    0 讨论(0)
  • 2020-12-03 14:35

    You are using some regex characters.

    Please escape them using \ or by using Pattern.quote method

    0 讨论(0)
  • 2020-12-03 14:43

    You have to escape . by \\.like following :

    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;
    
    0 讨论(0)
提交回复
热议问题