Jump to content

Using StringRegExp with binary cleartext


typhoon
 Share

Recommended Posts

I read in a file to $buffer, and then I try using StringRegExp on it. This works fine with a pure text file, but if the file is binary (with some clear text) the function seems to return prematurely (but without error). Specifically, for the file I'm reading in it only captures one match.

$array = StringRegExp($buffer, "path\d*:", 3)

I tried using binary mode with FileOpen, but in that case I get an invalid result code from StringRegExp.

I've tested the same code using a file that consists of only plain text; in that case it works.

Link to comment
Share on other sites

  • Moderators

I read in a file to $buffer, and then I try using StringRegExp on it. This works fine with a pure text file, but if the file is binary (with some clear text) the function seems to return prematurely (but without error). Specifically, for the file I'm reading in it only captures one match.

$array = StringRegExp($buffer, "path\d*:", 3)

I tried using binary mode with FileOpen, but in that case I get an invalid result code from StringRegExp.

I've tested the same code using a file that consists of only plain text; in that case it works.

You need to either have a string with no Nulls or you need to convert that string to binary ... Look at BinaryToString. Then you'll need to look for for your option in Hex ... path\d and your ":" will have to be the hex values.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Hi typhoon,

Just a note on Regular Expressions and AutoIt in case you didn't know.

AutoIt uses the PCRE (Perl Compatible Regular Expressions) engine.

If you are using google for tutorials or just searching for specifics, keep this in mind when looking for expressions or help on expressions.

Some things that may help you along with future questions or concerns:

QuickStart

Tutorials

RegExCoach

Link to comment
Share on other sites

  • Moderators

Without the file to test, I'd say this may be close:

Local $s_file = "Txt.txt"
Local $h_open = FileOpen($s_file, 16)
Local $sv_text = FileRead($h_open)
FileClose($h_open)
Local $s_path = __StringToHex("path")
Local $s_numbers = __StringToHex("0123456789")
Local $s_colon = __StringToHex(":")
Local $a_sre = StringRegExp($sv_text, "(?i)(" & $s_path & "(?:" & _
                StringTrimRight(StringRegExpReplace($s_numbers, "(.{2})", "\[\1\]\|"), 1) & ")*" & $s_colon & ")", 3)

Func __StringToHex($s_string)
    Return Hex(StringToBinary($s_string))
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...