Jump to content

How to pass regex backreferences to functions?


Recommended Posts

I'm writing a decoder for quoted-printable text. As part of the RFC, any character in quoted-printable text can be replaced by '=XX', where XX is the hex representation of the ASCII character. Usually this is done for whitespace and special characters; most actual text is displayed normally.

So, as part of my decoder I need to search through all of the text for patterns matcing '=XX', and replace it with the ASCII equivilant. I figure the easiest way to do it would be with StringRegExpReplace, but I can't figure out how to pass the detected '=XX' code to the chr() function. This is the best I was able to come up with:

$text = stringregexpreplace($text, "=([0-9A-F]{2})", chr(dec("\1")))

That properly identifies all of the hex codes, but the \1 is read as a literal string by dec() rather than as the backreferenced hex code. The result is that the hex codes are simply stripped out and replaced with nothing.

Just as a simple test to verify that my test statement is correct I tried this:

$text = stringregexpreplace($text, "=([0-9A-F]{2})", chr(dec("40")))

This works perfectly, replacing all hex codes with an @ sign. However, I still can't figure out how to pass the given hex code to dec().

Any suggestions for this? Is what I'm trying to do even possible in AutoIt? If not, how else can I accomplish this?

Thanks!

Link to comment
Share on other sites

Hi,

something like this?

#include<Array.au3>
$text = "=54 aaa a=53w=55q"

$returnArray = StringRegExp($text, "=([0-9A-F]{2})", 3)

_ArrayDisplay($returnArray, "")

For $i = 0 To UBound($returnArray) - 1
    $text = StringReplace($text, $returnArray[$i], Chr(Dec($returnArray[$i])))
Next

ConsoleWrite(@CRLF & $text)

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

That a good workaround. Thanks, th.meger!

I'm still a bit curious about my regexpreplace question, though. I can use th.meger's example to solve my current problem, but for future reference I'd like to know how I can, or if it's even possible to, manipulate the data in StringRegExpReplace with another function. Anyone know?

Thanks.

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...