Jump to content
Sign in to follow this  
JusGellin

StringRegExpReplace Ascii Value

Recommended Posts

JusGellin

I'm trying to substitute what's in brackets with an ascii character for the value that's in the brackets. For the following I want [05] to become ascii character 5.

So the string below would become equivilant to "abcdf" & chr(5) & "dfd". How can I do this?

The following doesn't work. Thanks

$Dat = "abdf[05]dfd"
ConsoleWrite(StringRegExpReplace($Dat, "\[(..)\]", chr("$1"))

Share this post


Link to post
Share on other sites
SmOke_N

I'm trying to substitute what's in brackets with an ascii character for the value that's in the brackets. For the following I want [05] to become ascii character 5.

So the string below would become equivilant to "abcdf" & chr(5) & "dfd". How can I do this?

The following doesn't work. Thanks

$Dat = "abdf[05]dfd"
ConsoleWrite(StringRegExpReplace($Dat, "\[(..)\]", chr("$1"))
You can't. Nested functions (ie... your Chr()) are fired from right to left, so Chr() is fired before StringRegExpReplace() even searches for your pattern.

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.

Share this post


Link to post
Share on other sites
Szhlopp

I'm trying to substitute what's in brackets with an ascii character for the value that's in the brackets. For the following I want [05] to become ascii character 5.

So the string below would become equivilant to "abcdf" & chr(5) & "dfd". How can I do this?

The following doesn't work. Thanks

$Dat = "abdf[05]dfd"
ConsoleWrite(StringRegExpReplace($Dat, "\[(..)\]", chr("$1"))
$String = "Test [79] cha[58]rs"

While 1
    $regexp = StringRegExp($String, "(?:\[)([0-9]*)(?:\])", 1)
    If UBound($regexp) >= 1 Then
        $regexp = Chr($regexp[0])
        $String = StringRegExpReplace($String, "(?:\[)([0-9]*)(?:\])", $regexp, 1)  
        
    Else
        ExitLoop
        
    EndIf
WEnd

MsgBox(0, "", $String)

Share this post


Link to post
Share on other sites
martin

I'm trying to substitute what's in brackets with an ascii character for the value that's in the brackets. For the following I want [05] to become ascii character 5.

So the string below would become equivilant to "abcdf" & chr(5) & "dfd". How can I do this?

The following doesn't work. Thanks

$Dat = "abdf[05]dfd"
ConsoleWrite(StringRegExpReplace($Dat, "\[(..)\]", chr("$1"))
$Dat = "abdf[05]dfd[13]"
$ans = StringRegExpReplaCE($dat,"(\[)(\d\d)(\])","chr(" & "\2" & ")")
ConsoleWrite($ans & @CRLF)
Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
JusGellin

Thanks for all the fast replies.

Thanks SmOke_N for the explanation why mine didn't work.

Szhlopp that is some really amazing clever code!!! It works great!!

martin I thought yours would work, but it must be like what SmOke_N said of how it is fired. So it literally puts the Chr in the result instead of the ascii character

Thanks again all.

Share this post


Link to post
Share on other sites
martin

Not StringRegExp for this.

Something else:

$Dat = "abdf[05]dfd[13]"

ConsoleWrite(Execute('"' & StringReplace(StringReplace($Dat, '[', '" & Chr('), ']', ') & "') & '"'))
That's no good if you have

$Dat = "abdf[05]dfd[13]hhh[dotty][187654.hgt]"
ConsoleWrite(StringReplace(StringReplace($Dat, '[', '" & Chr('), ']', ') & "'))

What was the Execute bit for ?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
JusGellin

Not StringRegExp for this.

Something else:

$Dat = "abdf[05]dfd[13]"

ConsoleWrite(Execute('"' & StringReplace(StringReplace($Dat, '[', '" & Chr('), ']', ') & "') & '"'))
Another great piece of code!!! Wow!!

I thought of using Execute, but it didn't work where I was putting it - for the substitute part. thanks

Edited by JusGellin

Share this post


Link to post
Share on other sites
martin

Thanks for all the fast replies.

Thanks SmOke_N for the explanation why mine didn't work.

Szhlopp that is some really amazing clever code!!! It works great!!

martin I thought yours would work, but it must be like what SmOke_N said of how it is fired. So it literally puts the Chr in the result instead of the ascii character

Thanks again all.

Oh, I misunderstood. I thought you wanted Chr(23) written there instead of [23].

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
JusGellin

Oh, I misunderstood. I thought you wanted Chr(23) written there instead of [23].

That's ok, sometimes it's hard to explain just what is being asked. Thanks

Share this post


Link to post
Share on other sites
Szhlopp

Thanks for all the fast replies.

Thanks SmOke_N for the explanation why mine didn't work.

Szhlopp that is some really amazing clever code!!! It works great!!

martin I thought yours would work, but it must be like what SmOke_N said of how it is fired. So it literally puts the Chr in the result instead of the ascii character

Thanks again all.

Np. Glad I could.

I don't know what tester you use but I've got a nice one in my sig FYI. ;)

Share this post


Link to post
Share on other sites
JusGellin

Not StringRegExp for this.

Something else:

$Dat = "abdf[05]dfd[13]"

ConsoleWrite(Execute('"' & StringReplace(StringReplace($Dat, '[', '" & Chr('), ']', ') & "') & '"'))
Could you please explain how this works? My mind is really boggled to try to figure this out. This is really deep. I would like to see how your thinking came up with this. Thanks

Share this post


Link to post
Share on other sites
trancexx

Could you please explain how this works? My mind is really boggled to try to figure this out. This is really deep. I would like to see how your thinking came up with this. Thanks

Sure

You said that you wanted to change "abdf[05]dfd" to "abcdf" & chr(5) & "dfd"

What that code does is replaces [ to " & chr( and then ] to ) & "

Then adds quotation marks at the beginning and to the end of that new string that is being executed afterward.

It's all in one line just to make an impression ( ;) ) :D, but to make it more understandable, here it is in full:

$Dat = "abdf[05]dfd[13]"

$Dat = StringReplace($Dat, '[', '" & Chr(') ;   $Dat = abdf" & Chr(05]dfd" & Chr(13]

$Dat = StringReplace($Dat, ']', ') & "') ;  $Dat = abdf" & Chr(05) & "dfd" & Chr(13) & " 

$Dat = '"' & $Dat ;  $Dat = "abdf" & Chr(05) & "dfd" & Chr(13) & "

$Dat = $Dat & '"' ;  $Dat = "abdf" & Chr(05) & "dfd" & Chr(13) & ""

$Dat = Execute($Dat) ; $Dat = abdfdfd & @CR

ConsoleWrite($Dat)oÝ÷ Ù8Z¶+jËZØʤ-«mêÞÂ)eméèº|×zn·
+Èî²Ú^­÷´w«{lJÚâ^Li²n¶*'ªí}ý²«¨¶)©®Þv«¨µ«­¢+ØÀÌØíÐôÅÕ½ÐílÀÕulÄÍtÅÕ½Ðì()
½¹Í½±]ɥѡáÕÑ ÌäìÅÕ½ÐìÌäìµÀìMÑÉ¥¹IáÁIÁ± ÀÌØíаÅÕ½Ðì ÀäÈíl¤ ÀäÈí¤ ÀäÈí¤ü ÀäÈí¤ü ÀäÈít¤ÅÕ½Ðì°ÌäìÅÕ½ÐìµÀì
¡È ÌäìµÀìÅÕ½ÐìÀÌØìÈÅÕ½ÐìµÀìÅÕ½ÐìÀÌØìÌÅÕ½ÐìµÀìÅÕ½ÐìÀÌØìÐÅÕ½ÐìµÀìÌä줵ÀìÅÕ½ÐìÌä줵ÀìÌäìÅÕ½ÐìÌä줤

This code will deal with all kind of inputs (good and bad ones, no need to check it).


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
martin

$Dat = "abdf[05]dfd[13]"

ConsoleWrite(Execute('"' & StringRegExpReplace($Dat,"(\[)(\d)(\d)?(\d)?(\])",'" & Chr(' & "$2" & "$3" & "$4" & ') & "') & '"'))

This code will deal with all kind of inputs (good and bad ones, no need to check it).

Now that I understand what was wanted, and why trancexx used execute, here is an improvement on the improved version by trancexx

$Dat = "abdf[05]dfd[13]"
ConsoleWrite(Execute('"' & StringRegExpReplace($Dat,"(\[)(\d{1,3})(\])",'" & Chr(' & "$2"  & ') & "') & '"'))
Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
JusGellin

Thank you very much, trancexx for explaining this. I'm a rookie with regex, but I'm loving them!

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.