Delete text and replace it with another. Topic is solved
Delete text and replace it with another.
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.
Re: Delete text and replace it with another.
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.
Re: Delete text and replace it with another.
@mikeyww, sorry, I just changed the content of my post. It can change things.
Re: Delete text and replace it with another.
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
Re: Delete text and replace it with another.
@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'.
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'.
Re: Delete text and replace it with another.
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
Re: Delete text and replace it with another.
@mikeyww, it doesn't work. All your code does is copy the entire string to the clipboard.
Re: Delete text and replace it with another.
Which string? left or right one (Assuming middle is @)?, and which text to look at the end for?, is it cursor?if the text cursor is at the end of such a string
Re: Delete text and replace it with another.
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
Re: Delete text and replace it with another.
@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.
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.
Re: Delete text and replace it with another.
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
Sorry about earlier posts: I realized that my text editor handles ^+{Left} differently than Notepad.
Re: Delete text and replace it with another.
@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.
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.
Re: Delete text and replace it with another.
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?
Re: Delete text and replace it with another.
@mikeyww, I tested the following string: Teststring!~[email protected].
Re: Delete text and replace it with another. Topic is solved
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
Re: Delete text and replace it with another.
@mikeyww, and now works perfect. Thank You!
Who is online
Users browsing this forum: marypoppins_1, mikeyww, OrangeCat, RussF and 138 guests