Delete text and replace it with another. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Delete text and replace it with another.

05 Aug 2021, 14:19

I have the following string: sersrdsdfsfdsre@momoimoijoioij. The sersrdsdfsfdsre and momoimoijoioij substrings are of different lengths and contain all sorts of signs in different cases. I'm looking for a method which, if the text cursor is at the end of such a string, after pressing the selected hotkey, it will delete all the characters following the @ sign (no matter how many characters it will contain) and insert the defined word in their place.
Last edited by Rostov on 05 Aug 2021, 14:32, edited 1 time in total.
User avatar
mikeyww
Posts: 26591
Joined: 09 Sep 2014, 18:38

Re: Delete text and replace it with another.

05 Aug 2021, 14:30

Code: Select all

find := "aaaaaaaaaa@bbbbbbbbbb", replace := "bicycle"
F3::
Clipboard =
Send +{Home}^c
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else Send % "{Right}" (RegExMatch(Clipboard, find "$") ? "^+{Left}" replace : "")
Return
Last edited by mikeyww on 05 Aug 2021, 14:36, edited 1 time in total.
Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Delete text and replace it with another.

05 Aug 2021, 14:34

@mikeyww, sorry, I just changed the content of my post. It can change things.
User avatar
mikeyww
Posts: 26591
Joined: 09 Sep 2014, 18:38

Re: Delete text and replace it with another.

05 Aug 2021, 14:37

Code: Select all

find := "sersrdsdfsfdsre@momoimoijoioij", replace := "bicycle"
F3::
Clipboard =
Send +{Home}^c
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else Send % "{Right}" (RegExMatch(Clipboard, find "$") ? "^+{Left}" replace : "")
Return
Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Delete text and replace it with another.

05 Aug 2021, 14:45

@mikeyww, the sersrdsdfsfdsre@momoimoijoioij string I provided is just an example. Each time the substrings to the left and right of the @ sign contain different characters and are of different lengths. So searching for the string I entered misses the point.

Basically, the idea is to remove the substring to the right of the @ sign (regardless of its content) and replace it with the word 'bicycle'.
User avatar
mikeyww
Posts: 26591
Joined: 09 Sep 2014, 18:38

Re: Delete text and replace it with another.

05 Aug 2021, 14:48

Code: Select all

replace := "bicycle"
F3::
Clipboard =
Send +{Home}^c
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else Send % "{Right}" (RegExMatch(Clipboard, "@\w+$") ? "^+{Left}" replace : "")
Return
Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Delete text and replace it with another.

05 Aug 2021, 14:53

@mikeyww, it doesn't work. All your code does is copy the entire string to the clipboard.
User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Delete text and replace it with another.

05 Aug 2021, 15:10

if the text cursor is at the end of such a string
Which string? left or right one (Assuming middle is @)?, and which text to look at the end for?, is it cursor?
User avatar
mikeyww
Posts: 26591
Joined: 09 Sep 2014, 18:38

Re: Delete text and replace it with another.

05 Aug 2021, 15:15

Another:

Code: Select all

replace := "bicycle"
F3::
Clipboard =
Send +{Home}^c
ClipWait, 0
Sleep, 50
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else SendInput % RegExMatch(Clipboard, "(.*@)\w+$", m) ? m1 replace : "{Right}"
Return
Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Delete text and replace it with another.

05 Aug 2021, 15:47

@mikeyww, it doesn't work as well. This code merely removes the substring to the right of the @ sign.

Maybe let's do it differently. Suppose we have a line of text containing the @ sign. There is a blinking text cursor at the end of this line. How do you move this cursor so that it is to the right of the @ sign? If I know that, I'll make the rest of the script myself.
User avatar
mikeyww
Posts: 26591
Joined: 09 Sep 2014, 18:38

Re: Delete text and replace it with another.

05 Aug 2021, 16:32

Code: Select all

F3::
Clipboard =
Send +{Home}^c
ClipWait, 0
Sleep, 50
If !ErrorLevel {
 Send {Right}
 RegExMatch(Clipboard, "(.*@)(\w+)$", m)
 SendInput % m1 > "" ? "{Left " StrLen(m2) "}" : ""
} Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return
Test it in Notepad.

Sorry about earlier posts: I realized that my text editor handles ^+{Left} differently than Notepad.
Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Delete text and replace it with another.

05 Aug 2021, 17:05

@mikeyww, it doesn't work again. Selects text, copies it, and places the cursor at a random position.

Please stick with my proposal. Have the code look for the @ character in the line of text and position the cursor to the right of it. I can handle the rest.
User avatar
mikeyww
Posts: 26591
Joined: 09 Sep 2014, 18:38

Re: Delete text and replace it with another.

05 Aug 2021, 17:22

So you went into Notepad, and tested this on a string. You used no other code in your script, and had no other scripts running. What string did you test?
Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Delete text and replace it with another.

05 Aug 2021, 17:28

@mikeyww, I tested the following string: Teststring!~[email protected].
User avatar
mikeyww
Posts: 26591
Joined: 09 Sep 2014, 18:38

Re: Delete text and replace it with another.  Topic is solved

05 Aug 2021, 17:54

OK:

Code: Select all

F3::
Clipboard =
Send +{Home}^c
ClipWait, 0
Sleep, 50
If !ErrorLevel {
 Send {Right}
 RegExMatch(Clipboard, "(.*@)(\S+)$", m)
 SendInput % m1 > "" ? "{Left " StrLen(m2) "}" : ""
} Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return
Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Delete text and replace it with another.

05 Aug 2021, 18:03

@mikeyww, and now works perfect. Thank You! :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: marypoppins_1, mikeyww, OrangeCat, RussF and 138 guests