[英]Perl is there any way to determine whether a text string contains binary data
您好,目前我在一個項目中,該項目需要從文本文件中讀取一些字符串並將這些字符串填充到XML文件中。 但是在此文件中,有些行包含二進制數據。 有沒有一種方法可以檢查字符串是否包含二進制數據?
在大多數情況下,以下正則表達式可能已足夠:
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:]]
-標點符號 Perl為此具有文件測試運算符: -T
和-B
。 但是請注意,僅僅因為字符串僅限於某些字符集並不意味着它就是我們認為的文本。 無論生成什么,它仍然可能是二進制數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.