开发者

Is it possible to match a regex against binary data in .net?

开发者 https://www.devze.com 2023-01-19 20:53 出处:网络
I know that I can use regex to match substrings in a string,but is it possible to match some patterns in binary data using regex?If so then in what format should the binary data be -binary array, stre

I know that I can use regex to match substrings in a string, but is it possible to match some patterns in binary data using regex? If so then in what format should the binary data be - binary array, stream, or something else?

edit:

well to explain i have binary data that shouldnt have some strings inside but the data itself is binary so i need to detect this pattern of data so i mark this data as invalid.

but i couldnt convert this binary data to string since it would be invalid. maybe only to some char[] or something.

edit:

now i am thinking maybe converting the binary data to a basic encoding (any hints on which is the most basic encoding available? certainly not unicode, i think ascii?) and then i will use regex.

but the question would i be able to convert any binary data to string using this encoding or i will encounter some cases which will be invalid and will cause exceptio开发者_JS百科ns when converting the binary data to string.


The technical answer to your question is yes, since you could just treat the binary data as a string of a particular encoding, but I don't believe that's what you're asking.

If you're asking if there's a library designed to do pattern matching on an array of bytes, then the .NET regex system will not do this and there isn't such a library that I'm aware of.


Yes it is possible but why would you want to? You would need to encode the data as a string first of course but if you are going to go to that trouble why don't you simply deserialize the data into a more sensible data structure?

Regular expressions are for matching strings only - if have binary data then you can be quite sure that a regex is the wrong solution to your problem.


I haven't tried this, but I'll bet you could convert your binary data to a base64 string, then use a regex to find your search string - of course, you would have to encode your search string in base64 as well.


Perl are able to match against pure binary data. And I think this should be possible with most of the other brands.

You can use the abbreviation '\xNN' to search for a particular byte in its hexadecimal form. So even char-classes like '[\x20-\xff]' are possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消