在Java中处理带有特殊字符的字符串

时间:2019-02-18 17:20:51

标签: java json string escaping special-characters

我正在实现一个字符串匹配算法,该算法需要处理带有特殊字符的字符串。匹配的一方面,字符串是用Python准备的,然后经过了JAVA。另一方面,它们是由另一个环境准备的。现在,我在Java程序中匹配它们(从JSON输入检索的字符串)。

虽然处理了某些字符,但在处理许多其他字符时却遇到了问题。

例如,我为此得到一个 MATCH (两者在我的控制台上都显示为>> AS IT COMES CRUMBLING):

"text":"\u003e\u003e AS IT COMES CRUMBLING"
"caption":">> AS IT COMES CRUMBLING"

但是这些显示为 NON-MATCH

"text":"What if you had fewer headaches\nand migraines a month?"
"text":"What if you had fewer headaches\\nand migraines a month?"

或者这个:

"text":"Effects of BOTOX® may spread"
"text":"Effects of BOTOX\\xc2\\xae may spread"

或者这个:

"text":"Let's also rethink how\nwe care for ourselves."
"text":"Let'\\xe2\\x80\\x99s also rethink how\\nwe care for ourselves."

在我的代码中,我使用JSONPath从两侧读取JSON的输入,将它们放在ArrayList中,然后将其与列表中的所有项目进行比较。

boolean found=false;
myText foundText = null;
for (int i = 0; i < scheduledText.size(); i++) {
    if(current.text.equals(scheduledText.get(i).text)) {
        found = true;
        foundText =scheduledText.get(i);
        break;
    }
}
if(found)
   //print MATCH
else
   //print NON_MATCH

我很沮丧。我该怎么办?我该如何处理?

1 个答案:

答案 0 :(得分:1)

因此,对于我提出的解决方案,您将在Java代码中使用一个函数,如下所示。

private static String cleanTextContent(String text)
    {
        // strips off all non-ASCII characters
        text = text.replaceAll("[^\\x00-\\x7F]", "");

        // erases all the ASCII control characters
        text = text.replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "");

        // removes non-printable characters from Unicode
        text = text.replaceAll("\\p{C}", "");

        text = text.replaceAll("[^ -~]","");

        text = text.replaceAll("[^\\p{ASCII}]", "");

        text = text.replaceAll("\\\\x\\p{XDigit}{2}", "");

        text = text.replaceAll("\\n","");

        text = text.replaceAll("[^\\x20-\\x7e]", "");
        return text.trim();
    }

调用此函数后,您可以使用Apache Commons lib将字符串转换为md5哈希,如下所示。

private static String hashMyString(String text)  {

    String hashText= text;

    String md5Hex = DigestUtils
      .md5Hex(hashText).toUpperCase();

   return md5Hex;
}

最后,只需比较主程序中的两个哈希即可。

编辑:如果使用的是maven,则基本上使DigestUtils正常工作。

   <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.9</version>
</dependency>

编辑: 我完整的String测试代码。

public class App 
{
    public static void main( String[] args ) throws UnsupportedEncodingException
    {

       String sideOneString = "Effects of BOTOX® may spread";
       String sideTwoString = "Effects of BOTOX\\xc2\\xae may spread";
       String sideThreeString = "BOTOX injections take about\n15 mins";
       String sideFourString  = "BOTOX\\xc2\\xae injections take about\\n15 mins";


       System.out.println( hashMyString(cleanTextContent(sideOneString)));
       System.out.println( hashMyString(cleanTextContent(sideTwoString)));
       System.out.println( hashMyString(cleanTextContent(sideThreeString)));
       System.out.println( hashMyString(cleanTextContent(sideFourString)));
    }





    private  static  String hashMyString(String text)  {

        String hashText= text;

        String md5Hex = DigestUtils.md5Hex(hashText).toUpperCase();
        //System.out.println(md5Hex);
       return md5Hex;
    }

    private static String cleanTextContent(String text)
    {
        // strips off all non-ASCII characters
        text = text.replaceAll("[^\\x00-\\x7F]", "");

        // erases all the ASCII control characters
        text = text.replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "");

        // removes non-printable characters from Unicode
        text = text.replaceAll("\\p{C}", "");

        text = text.replaceAll("[^ -~]","");

        text = text.replaceAll("[^\\p{ASCII}]", "");

        text = text.replaceAll("\\\\x\\p{XDigit}{2}", "");
        text = text.replaceAll("\\\\n","");


        text = text.replaceAll("[^\\x20-\\x7e]", "");
        return text.trim();
    }
}

结果:

F928A529F380EB59575AC8A175FDFE79
F928A529F380EB59575AC8A175FDFE79
B4740299C53E18C9ECAF18BA35151D43
B4740299C53E18C9ECAF18BA35151D43