【问题标题】:Perl is there any way to determine whether a text string contains binary dataPerl有没有办法判断一个文本字符串是否包含二进制数据
【发布时间】:2014-02-09 08:07:47
【问题描述】:

您好,目前我正在进行一个需要从文本文件中读取一些字符串并将这些字符串填充到 XML 文件中的项目。但是在这个文件中有一些包含二进制数据的行。有没有办法可以检查字符串是否包含二进制数据?

【问题讨论】:

    标签: string perl binary


    【解决方案1】:

    在大多数情况下,以下正则表达式可能就足够了:

    m{
        [^          # Consider the match a success if each of the following meta-characters do not match
            \w          # Match word
            \d          # Match digit
            \s          # Match whitespace
            [:punct:]   # Match punctuation
        ]
    }x
    

    本质上,文本必须至少包含一个不匹配的(二进制)字符:

    • \w - 字
    • \d - 数字
    • \s - 空格
    • [[:punct:]] - 标点符号

    【讨论】:

    • 感谢 Shlomi Fish 建议对字符类进行否定。
    • /[^[:print:][:space:]]/
    【解决方案2】:

    Perl 对此有文件测试运算符:-T-B。但是请注意,仅仅因为字符串仅限于特定的字符集并不意味着它就是我们认为的文本。它仍然可能是生成它的二进制数据。

    【讨论】:

      猜你喜欢
      • 2014-06-18
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 2011-02-05
      相关资源
      最近更新 更多