Javaでの特殊文字で文字列を処理します

ティナJ:

私は特殊文字で文字列を処理する必要が文字列照合アルゴリズムを実装しています。マッチングの一方の側に、文字列は、JAVAを経て、その後、Pythonで調製しました。他の側では、彼らは別の環境により調製しました。今、私は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?"

それとも、この1:

"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、その後、リスト内のすべての項目に対して1を比較します。

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

私はイライラしています。私は何をすべきか?私はこれらをどのように扱うことができますか?

ミゲル:

だから私の提案されたソリューションのために、あなたは以下のような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();
    }

あなたはこの関数を呼び出したら、このようなMD5ハッシュ何かに変換文字列にはApache CommonsのLIBを使用することができます。

private static String hashMyString(String text)  {

    String hashText= text;

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

   return md5Hex;
}

最後に、ちょうどあなたのメインプログラムで2つのハッシュを比較します。

編集: Mavenの使用している場合、これは基本的にDigestUtilsの仕事を作るライブラリがあります。

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

編集:文字列のための私の完全なテストコード。

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

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=215290&siteId=1
おすすめ