Sign in to follow this  
Followers 0
AutID

StringRegExpReplace char "0x$1"

5 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Because the expression above is bad

The correct way should be this

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

$sString =  Execute("'" & StringRegexpReplace($sString, "%([0-9a-fA-F]{2})", "'& Chr(0x$1) &'") & "'")
ConsoleWrite($sString & @LF)

Share this post


Link to post
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
Sign in to follow this  
Followers 0