Regular expression search for C # byte[] Arrays

Source: Internet
Author: User
Tags expression engine

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.