RegExHotstring - dynamic RegEx Hotstrings

Post your working scripts, libraries and tools.
8LWXpg
Posts: 40
Joined: 26 May 2022, 03:55

RegExHotstring - dynamic RegEx Hotstrings

Post by 8LWXpg » 28 May 2022, 10:14

I wrote a simple script that can use RegEx in hotstring trigger.

https://github.com/8LWXpg/RegExHotstring

uses space, tab or enter to trigger hotstring

simple demo:

Code: Select all

#Include RegExHotstring.ahk
#SingleInstance Force

; the upmost function will be triggered first

; replace with regex string
RegExHotstring("(\w+)a", "b$1", "C")
RegExHotstring("(\w)a(\w)", "$2a$1", "*")
RegExHotstring("(\d+)(\w+)", "$2$1", "OB0")
RegExHotstring("U\+([0-9A-F]{4})", "{U+$1}", "C")

; use anonymous function
RegExHotstring("a(\w)c", (match) => MsgBox("you just typed a" match[1] "c!"), "*?B0")
RegExHotstring("\w+b", (*) => Send("{Enter}"))

; modify callback and options
^!a:: RegExHotstring("(\w+)a", (match) => MsgBox("matched: " match[1]), "*")

; call with function name
RegExHotstring("(\w*)c", call)
RegExHotstring("r(\d+)", rand)

; receives RegExMatchInfo
call(match) {
	MsgBox("matched: " match[1])
}

rand(match) {
	static char := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	static char_len := StrLen(char)
	loop match[1] {
		r := Random(1, char_len)
		str .= SubStr(char, r, 1)
	}
	SendText(str)
}
Feel free to ask questions or open an issue on GitHub.
Last edited by 8LWXpg on 01 Sep 2023, 08:15, edited 4 times in total.

8LWXpg
Posts: 40
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 05 Feb 2023, 00:46

I have recently revised this script and made some improvements, including:
  • add tab and enter as trigger keys
  • improve code quality and fix bugs
it should now work almost like a normal hotstring.

The reasons I make these changes so late is mostly because I didn't find many usages of this script and only use it as a random string generator sometimes. So, if you find the script useful, please let me know!

oldstone
Posts: 13
Joined: 28 Oct 2021, 07:29

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by oldstone » 05 Feb 2023, 09:47

When a hot string consists of multiple characters, can it be set to no end key?

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

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by mikeyww » 05 Feb 2023, 11:15

Options:
* (asterisk): An ending character (e.g. Space, ., or Enter) is not required to trigger the hotstring.

8LWXpg
Posts: 40
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 05 Feb 2023, 12:26

oldstone wrote:
05 Feb 2023, 09:47
When a hot string consists of multiple characters, can it be set to no end key?
No, and its currently no options available. Except for those built-in for RegEx https://www.autohotkey.com/docs/v2/misc/RegEx-QuickRef.htm#Options.

Maybe I will work with some of the settings, but it needs to change the trigger key and matching behavior.

8LWXpg
Posts: 40
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 07 Feb 2023, 03:33

8LWXpg wrote:
05 Feb 2023, 12:26
oldstone wrote:
05 Feb 2023, 09:47
When a hot string consists of multiple characters, can it be set to no end key?
No, and its currently no options available. Except for those built-in for RegEx https://www.autohotkey.com/docs/v2/misc/RegEx-QuickRef.htm#Options.

Maybe I will work with some of the settings, but it needs to change the trigger key and matching behavior.
I also found that no ending characters will lead to some issues. For example, if you have "\d*", it will trigger every time. So, I think it's better to have ending keys with RegEx.

8LWXpg
Posts: 40
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 10 Feb 2023, 05:15



User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by kunkel321 » 17 Feb 2023, 17:52

Thanks for sharing your work. Sorry being a newb, but how does a person use this? I see the bundled demo script, but I don't recognize anything in there as a Hotstring. I do understand what a RegEx is...
ste(phen|ve) kunkel

8LWXpg
Posts: 40
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 17 Feb 2023, 23:40

kunkel321 wrote:
17 Feb 2023, 17:52
Thanks for sharing your work. Sorry being a newb, but how does a person use this? I see the bundled demo script, but I don't recognize anything in there as a Hotstring. I do understand what a RegEx is...
The script includes a function that allows you create RegExHotstrings, it's not likes normal hotstrings, and it relies on the function to control its behavior.

Code: Select all

RegExHotstring("(\w+)a", "b$1")
Take this line as example, if you type "ccca" then press enter, it will replace as "bccc", works like RegExReplace.

Code: Select all

RegExHotstring("(\w*)c", call)
call(m) {
	MsgBox("matched: " m[1])
}
For these lines of code, type "abc" then enter, the function named "call" will receive an argument as "m" and its data type is RegExMatchInfo.

User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by kunkel321 » 20 Feb 2023, 12:56

Yes, I see. Thank you for the explanation!
ste(phen|ve) kunkel

frabjous
Posts: 10
Joined: 30 Mar 2014, 16:44

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by frabjous » 20 Feb 2023, 16:25

This script is excellent but I am having trouble creating a dynamic hotstring which is triggered by either a space or a punctuation mark at the end of the entered text. I thought I could do this with the recently added "no end key" option and including the punctuation marks in the hotstring (along the lines "[.,;: ]") but then I cannot make it work when a space is typed. Am I missing something?

8LWXpg
Posts: 40
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 20 Feb 2023, 23:50

frabjous wrote:
20 Feb 2023, 16:25
This script is excellent but I am having trouble creating a dynamic hotstring which is triggered by either a space or a punctuation mark at the end of the entered text. I thought I could do this with the recently added "no end key" option and including the punctuation marks in the hotstring (along the lines "[.,;: ]") but then I cannot make it work when a space is typed. Am I missing something?
It's currently unable to match white space characters ("\s" in RegEx) because it's used in trigger without "*" option. But I can make a update to make it triggers white space characters in the end.

8LWXpg
Posts: 40
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 23 Feb 2023, 08:35

frabjous wrote:
20 Feb 2023, 16:25
This script is excellent but I am having trouble creating a dynamic hotstring which is triggered by either a space or a punctuation mark at the end of the entered text. I thought I could do this with the recently added "no end key" option and including the punctuation marks in the hotstring (along the lines "[.,;: ]") but then I cannot make it work when a space is typed. Am I missing something?
current work around is to make two separate hotstrings, one take "[.,;:]" at the end and use "*" option, another doesn't.

frabjous
Posts: 10
Joined: 30 Mar 2014, 16:44

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by frabjous » 23 Feb 2023, 11:20

Thank you, 8LWXpg. The work around does all that is needed!

frabjous
Posts: 10
Joined: 30 Mar 2014, 16:44

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by frabjous » 27 Feb 2023, 16:30

I have hit another problem! If I add "#Include RegExHotstring.ahk" to an existing ahk script with other hotstrings, so that I can include some dynamic RegEx hotstrings, all the normal hotstrings in the script stop working. The workaround is presumably to keep the dynamic hotstrings in a separate script, but it would be preferable to keep all my hotstrings in one script. Is that possible?

8LWXpg
Posts: 40
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 28 Feb 2023, 00:38

modify SendLevel to allow trigger normal hotstrings in the same script

https://github.com/8LWXpg/RegExHotstring/releases/tag/v2.5

frabjous
Posts: 10
Joined: 30 Mar 2014, 16:44

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by frabjous » 28 Feb 2023, 04:28

Many thanks. That adjustment does the trick!

EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 21 Apr 2023, 13:01

I have been moving along happily with a stale installation of AHK and only recently discovered the v2. One thing I have had concerns about is using RegEx with the Hotstrings. This looks like what I need and I'll be playing with it shortly.

Here is what I was using in the past:
https://www.autohotkey.com/board/topic/114764-regex-dynamic-hotstrings/

The relevance might not be high, but here is how I adapted that to serve my needs with various terminal characters to provide consistent results:

Code: Select all

tailchar := "(\s|-|\(|\)|\[|\]|\{|\}|:|\\|\?|!|""|\.|,)"
hotstrings("\b(F|f)lam"tailchar,"%$1%lammable storage cabinet%$2%")
;Followed by several 10s and more of these triggers
I'll likely use the Options provided by OP to retain this functionality as I upgrade to AHK2. Thank you for the product and thank you for the discussion following!

EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 21 Apr 2023, 15:26

Well folks, I didn't get very far.
I rebuilt some of my AHK1 to be AHK2. No heavy lifting required. I cannot get this script to load though. Basics, I know!
I built a test.ahk to test #include and it works.
If I #include RegExHotstring.ahk all my backspaces get doubled. Seems maybe a second instance is sneaking in there, can't tell where.
If I reload AHK after commenting out the #include RegExHotstring.ahk, it returns to normal single backspace behaviour.
Likely some initialization problem. Could you help me see the obvious error I'm making?

AutoHotKey64.AHK (Main entry point)

Code: Select all

#include test.ahk
;#Include RegExHotstring.ahk
#SingleInstance Force

SetTimer MoveMouse, 5000

;Timers
MoveMouse()
{
    If ( A_TimeIdle > 59999 ) {
        MouseMove 10, 10,,"R"
        MouseMove -10,-10,,"R"
    }
}

;Hotkeys
~NumpadSub & ~numpad9:: ; - and 9 on the numpad
{
    shutdown 0
}

~#q::
{
    Reload
} 

Test.AHK

Code: Select all

^!1::msgbox "testing"

Post Reply

Return to “Scripts and Functions (v2)”