Jump to content

StringRegExpReplace char "0x$1"


AutID
 Share

Recommended Posts

With what does this pattern replace the captured chars?

Local $sString = "Th%is is %a t%est s%tring"
$sString = StringRegExpReplace ( $sString, "%([0-9a-fA-F]+)", Execute ( Chr ( '0x$1' ) ) )
ConsoleWrite($sString & @LF)

In the example it replaces them with empty string. However this doesn't look too logic to me. If it was the case then the third param of of the reg replace would simply be an empty string.

I found this example on internet.

 

Edited by AutID
Link to comment
Share on other sites

Your code will try to change the regex matches by the result value of the Execute command. Chr ( '0x$1' ) amounts to NUL (ascii char 0), thus the Execute command will try to execute the NUL character. That fails, hence the return value of Execute is "" (as noted in the help file).

If you remove the Execute part but leave the Chr ( '0x$1' ) part, the string will get broken off at the first occurance of the regex match, because the match then still gets replaced by NUL, aka a terminator.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Sometimes in URLs special characters are replaced by a % followed by their hex value (two hex chars), e.g. %20 for the space

Probably this code is made to convert them back to their ascii representation

Local $sString = "Th%20is is %20a t%20est s%20tring"

$sString = StringRegExpReplace ( $sString, "%([0-9a-fA-F]{2})", Execute(Chr('0x$1')) )
ConsoleWrite($sString & @LF)
Edited by mikell
Link to comment
Share on other sites

I thought the same and it concerns URLs. So in this case they are replaced by and empty string? If yes then why it is like it is and not just an empty string?
 

Edit: like this

Local $sString = "Th%20is is %20a t%20est s%20tring"
$sString = StringRegExpReplace ( $sString, "%([0-9a-fA-F]{2})", "" )
ConsoleWrite($sString & @LF)

  Edited by AutID
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...