Jump to content

Catchy Replace example


Deye
 Share

Recommended Posts

Replace single characters on the left with anything specified between matching vertical bars on the right


$txt = "Jack's birthday is 02/02/02 & Jill's is the same"

MsgBox(0, '', _PatternStringReplace($txt, "'/ &", "|-|_|And"))

MsgBox(0, '', _PatternStringReplace($txt, "/&20", "2|could it be that"))

MsgBox(0, '', _PatternStringReplace($txt, "j02/&'", "M"))

Func _PatternStringReplace($sTxt, $sPatt, $sReplace)
    Local $a1 = StringSplit($sPatt, "")
    Local $a2 = StringSplit($sReplace, "|")
    ReDim $a2[UBound($a1)]
    Local $sda = ObjCreate("Scripting.Dictionary")
    For $i = 1 To UBound($a1) - 1
        $sda.item($a1[$i]) = $a2[$i]
        If StringIsAlpha($a1[$i]) Then $sda.item(StringIsLower($a1[$i]) ? StringUpper($a1[$i]) : StringLower($a1[$i])) = $a2[$i]
    Next
    Return Execute('"' & StringRegExpReplace($sTxt, "(?i)([" & $sPatt & "])", '" & $sda.Item("$1") & "') & '"')
EndFunc
Edited by Deye
Link to comment
Share on other sites

This could be a useful script. Basically, it would serve as an easy version of StringRegExpReplace. If you are open to a couple suggestions, here's my thoughts:

1. To make it more standard like other AutoIt functions, don't use @Tab as a separator. Instead, make the data to the right of the @Tab into a third parameter. Here's how that could work:

Spoiler

$txt = "Jack's birthday is 02/02/02 & Jill's is the same"

MsgBox(0, '', _Replace($txt, "/| |&", "-|_|And"))

Func _Replace($sTxt, $sSearch, $sReplace)
    Local $a1 = StringSplit($sSearch, "|")
    Local $a2 = StringSplit($sReplace, "|")
    Local $sda = ObjCreate("Scripting.Dictionary")
    For $i = 1 To $a1[0]
        $sda.item($a1[$i]) = $a2[$i]
    Next
    Return Execute('"' & StringRegExpReplace($sTxt, "([" & $sSearch & "])", '" & $sda.Item("$1") & "') & '"')
EndFunc

2. Add code to handle errors. If the user inputs more or less items to search than replace, it could break the script. Also, maybe even consider returning error codes.

3. Consider adding a GUI element where you could paste your sentence from the clipboard, specify what you want to replace and it's replacement values, and display the output sentence (and maybe have a copy to clipboard button).

It looks like you are off to a good start. These are just ideas in case you want to develop your script further, you don't have to do any of them.

Thanks for sharing!

Link to comment
Share on other sites

 

8 hours ago, abberration said:

MsgBox(0, '', _Replace($txt, "/| |&", "-|_|And"))

you didn't put\treat the pattern part correctly, doing it like that would replace all vertical bars found in a text unintentionally

This is an example for treating\replacing single characters (as from a StringRegExp pattern) with delimited subjects to the right

with you're recommendation I have addressed 1 & 2 -  (Added code to mildly bypass any array errors and added something to address an upper lower case issue)

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