【问题标题】:Perl, substitute hexa text by the corresponding chr-ed characterPerl,用相应的字符替换十六进制文本
【发布时间】:2013-10-16 12:07:31
【问题描述】:

我有一个文件,其中包含由文本和编码字符混合而成的长行。

%255D%252C%2522actualPage%2522%253A1%252C%2522rowPerPage%2522%253A50%257D%255D

每个编码器字符是%25xx,其中xx 是ASCII 字符的十六进制值(例如%2540 = @)

我尝试了以下但没有成功

perl -pe 's/%25([0-9A-F](0-9A-F])/\x$1/' myfile.txt
perl -pe 's/%25([0-9A-F](0-9A-F])/chr($1)/' myfile.txt

你有什么线索吗?

TIA,佩尔

【问题讨论】:

    标签: regex perl chr


    【解决方案1】:

    也许你想要的是URI::Encode。为此使用模块比使用正则表达式更好。

    perl -MURI::Encode -nle'$u=URI::Encode->new(); print $u->decode($u->decode($_));'
    

    输入字符串的输出是这样的:

    ],"actualPage":1,"rowPerPage":50}]
    

    您会注意到,字符串必须被解码两次,因为它已经被编码了两次(%25 显然是百分号%)。临时输出是

    %5D%2C%22actualPage%22%3A1%2C%22rowPerPage%22%3A50%7D%5D
    

    【讨论】:

      【解决方案2】:
      perl -MURI::Escape -ne'print uri_unescape(uri_unescape($_))'
      

      【讨论】:

        【解决方案3】:
        perl -pe 's/%25([0-9A-F][0-9A-F])/chr hex $1/ge' myfile.txt
        

        输出

        ],"actualPage":1,"rowPerPage":50}]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-02-22
          • 1970-01-01
          • 2018-11-12
          • 2013-08-08
          • 2011-12-07
          • 2018-03-05
          • 2018-06-12
          • 1970-01-01
          相关资源
          最近更新 更多