How can I fix the issue of Regular Expressions ? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
hancre
Posts: 251
Joined: 02 Jul 2021, 20:51

How can I fix the issue of Regular Expressions ?

Post by hancre » 04 Mar 2024, 03:07

When I try 1,2,3, and 4 in the menu of this following script, it doesn't change any text.
when I try 7, it adds the menu text 'Add Inputdata to beginning', not input data to the beginning.
How can I fix it ? Thank you for your help in advance.

Code: Select all

MyMenu := Menu()
MyMenu.Add("1. Remove the inputdata", RemoveInputdata)
MyMenu.Add("2. Remove the lines with inputdata", RemoveLinesWithInputdata)
MyMenu.Add("3. Remove Left with inputdata", RemoveLeftWithInputdata)
MyMenu.Add("4. Remove left without inputdata", KeepLeftWithInputdata)
MyMenu.Add("7. Add Inputdata to beginning", AddToBeginning)

Capslock & f:: {    
    A_Clipboard := ""
    Sleep 100
    Send('^c') ; option 2
    ClipWait
        global userinput := Inputbox("Enter Text", "Inputbox")
    If (userinput = "") ; If blank/cancel pressed
        Return
    MyMenu.Show() ; MyMenu 표시
    Sleep 100 ; 
    Send('^v') 
}


RemoveInputdata(userInput, *) {   ; Case 1 
    RegExEscape(str) {
    return StrReplace(str, "[]\/.*+?()|^${}", "`$&")
}
A_Clipboard := RegExReplace(A_Clipboard, ".*" . RegExEscape(userInput) . ".*", "")
}


RemoveLinesWithInputdata(userInput, *) {  ; case 2 
    A_Clipboard := RegExReplace(A_Clipboard, "mUO.*\Q" userInput "\E.*", "")
}



RemoveLeftWithInputdata(userInput, *) {  ; case 3 
   A_Clipboard := RegExReplace(A_Clipboard,"mUO`a).*?\Q" userInput "\E", "")
}

KeepLeftWithInputdata(userInput, *) {  ; case 4 
   A_Clipboard := RegExReplace(A_Clipboard,"`aim)^.*(\Q" userInput "\E)","$1")
}


AddToBeginning(userInput, *) { ; case 7 
    Loop parse, A_Clipboard, "`n", "`r" {
        prefix_apl := userInput . A_LoopField . "`n"
        outcome .= prefix_apl
        Sleep 50
    }
   A_Clipboard := outcome
}

just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How can I fix the issue of Regular Expressions ?

Post by just me » 04 Mar 2024, 04:35

Menu callback functions have three default parameters:

Code: Select all

MyCallback(ItemName, ItemPos, MyMenu) { ...
The first parameter contains the item's name regardless how you call it.

You defined userInput as global in the hotkey function. So you can read the variable in every other function without further ado. So just remove the name from the parameter list and consider that userInput is an object.

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: How can I fix the issue of Regular Expressions ?

Post by mikeyww » 04 Mar 2024, 04:41

An example is below. Sending text occurs inside the function in this example. I recommend reading the documentation pages for InputBox and Menu. These pages explain and demonstrate how to work with these objects.

Code: Select all

#Requires AutoHotkey v2.0
m := Menu()
m.Add '1. Remove the inputdata', removeInputdata

F3:: {
 Global ib
 Clipboard := '', Send('^c')
 If ClipWait(1) {
  txt := SubStr(A_Clipboard, 1, 65), (txt != A_Clipboard) && txt .= '...'
  ib  := InputBox('Clipboard:`n' txt '`n`nEnter text to change.', 'Change text', 'w530 h150')
  If ib.Result = 'OK' && ib.Value != ''
   m.Show
 } Else MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 'Icon!'
}

removeInputdata(itemName, itemPos, m) {
 SendText RegExReplace(A_Clipboard, ib.Value, 'bbbbbb')
}

hancre
Posts: 251
Joined: 02 Jul 2021, 20:51

Re: How can I fix the issue of Regular Expressions ?

Post by hancre » 04 Mar 2024, 22:12

mikeyww wrote:
04 Mar 2024, 04:41
the menu of your code is simpler than mine. Good. ^^
But I can't get the results for other menus.
I tried these following functions. but they don't work.

Code: Select all

RemoveLinesWithInputdata(itemName, itemPos, m) {  ; Remove the lines with inputdata 
 A_Clipboard := RegExReplace(A_Clipboard, "mUO.*" ib.Value ".*`n", "")
}

RemoveLeftWithInputdata(itemName, itemPos, m) {  ; Remove Left with inputdata 
    A_Clipboard := RegExReplace(A_Clipboard, "mUO`a).*?" ib.Value, "")
}

KeepLeftWithInputdata(itemName, itemPos, m) { ; Remove left without inputdata 
    A_Clipboard := RegExReplace(A_Clipboard, "`aim)^.*(\Q" ib.Value "\E)", "$1")
}

AddToBeginning(itemName, itemPos, m) { ; Add inputdata to the beginning of every line.
    Loop parse, A_Clipboard, "`n", "`r" {
        prefix_apl := ib.Value . A_LoopField . "`n"
        outcome .= prefix_apl
        Sleep 50
    }
   A_Clipboard := outcome
Last edited by hancre on 04 Mar 2024, 22:21, edited 1 time in total.

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: How can I fix the issue of Regular Expressions ?

Post by mikeyww » 04 Mar 2024, 22:20

As a problem statement, "doesn't work" doesn't work.

What is the script?
What is the input?
What output is expected?

hancre
Posts: 251
Joined: 02 Jul 2021, 20:51

Re: How can I fix the issue of Regular Expressions ?

Post by hancre » 04 Mar 2024, 22:31

@mikeyww
Sorry. I edited my post during your first reply.

Could you review my first revised thread ?
< it doesn't work > means < the function doesn't work and show any change.

I'll explain what I want with this example.

Clipboard :
MyMenu.Add("2.Remove the lines with inputdata", RemoveLinesWithInputdata)
MyMenu.Add("3. Remove Left with inputdata", RemoveLeftWithInputdata)
MyMenu.Add("4. Remove left without inputdata", KeepLeftWithInputdata)
MyMenu.Add("7. Add Inputdata to beginning", AddToBeginning)

Inputdata : Remove

What I want :
no.2 should remove 1st, 2nd, 3rd lines and keep the 4th lines.
no.3 should remove With and the left part of it
ex. 'MyMenu.Add("2. Remove : removed.
the lines with inputdata", RemoveLinesWithInputdata) : not removed.
no.4 should keep 'Remove' in the every line and remove 'MyMenu.Add("2.'
no.7 should add 'Remove' to the every line.
ex. RemoveMyMenu.Add("2.Remove the lines with inputdata", RemoveLinesWithInputdata)

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: How can I fix the issue of Regular Expressions ?  Topic is solved

Post by mikeyww » 05 Mar 2024, 06:48

I did not go back to see which posts here changed, and what changed about them. In using one of the needed actions, below is an example of how to add a string to the start of the clipboard. I don't plan to write the entire script for you, or to post a tutorial about regular expressions. Perhaps someone else wants to do these things!

Code: Select all

addToStart(itemName, itemPos, m) {
 A_Clipboard := ib.Value ' ' A_Clipboard
 SoundBeep 1500
}

Code: Select all

removeWord(itemName, itemPos, m) {
 A_Clipboard := RegExReplace(A_Clipboard, '\b' ib.Value '\b\h*')
 SoundBeep 1500
}

hancre
Posts: 251
Joined: 02 Jul 2021, 20:51

Re: How can I fix the issue of Regular Expressions ?

Post by hancre » 06 Mar 2024, 00:42

Thanks a lot for your help. ^^;; I've got what I want thanks to your tip. ^^

Post Reply

Return to “Ask for Help (v2)”