Matches a regular expression at a specific location in the byte[] array.
In order to search for a specific type of file from your hard disk, you need to match the file's characteristic values.
For files that have mastered the structure of the file, the Hard-code method is used to match;
For files that do not have a file structure, a regular expression is used to match the file's characteristic data. The disadvantage is that the matching speed is slow. However, the development time can be saved compared to reading or deciphering the data structure of each file.
Because the regular expression engine in C # only provides a regular match to a string, and the data we read from the hard disk is of type byte[], we need to add support for regular matching of the byte[] data.
In addition, the eigenvalues of the file (otherwise difficult to call "feature") appear in a fixed position, all to specify a regular match at a specific location.
1 usingSystem.Text.RegularExpressions; 2 3 //byte[] Array grep Search to determine whether a regular expression string is matched at the specified offset4 //buffer: byte[] Array to search for5 //regex: The regular expression string to match6 //Offset:buffer The offset of an array7 Public Static BOOLBinaryregex (byte[] Buffer,stringRegexintoffset)8 {9list<Char> charList =Newlist<Char>();Ten foreach(byteBinchbuffer) One { ACharlist.add ((Char) b); - } - stringbstring =NewString (Charlist.toarray ()); theMatch match =Regex.match (Bstring,regex); - if(Match. Success && match. Index = =offset) - return true; - Else return false; +}
Regular expression search for C # byte[] Arrays