RegExHotstring - dynamic RegEx Hotstrings

Post your working scripts, libraries and tools.
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 24 Apr 2023, 08:22

Thank you much for the update!!

Might there be a way that I've not found to extend the terminal character set that activates the replacement? I have appended (\s|-|\(|\)|\[|\]|\{|\}|:|\\|\?|!|`"|\.|,) as seen here:

Code: Select all

RegExHotstring("\b([0-9])yr(\s|/|-|\(|\)|\[|\]|\{|\}|:|\\|\?|!|`"|\.|,)","$1-year$2","*")
;or
RegExHotstring("\b([0-9])yr(\s|\W)","$1-year$2","*")
and it responds in cases I've tried (the expanded list above) except \s.

Typing 3yr should expand to 3-year. It behaves like so:
3yr- becomes 3-year-
3yr[ becomes 3-year[
3yr{space or enter} stays 3yr

My goal is that the nonword characters trigger the expansion. I have played with replacing the list with /W and it seems to work with the exception of /s still. I'd previously put the list in manually as the last script I used for this failed with /W.

Further, with the list above or with /W, the following behavior is noted:
3yr! becomes 3-year (the ! is not brought to the end)
{, }, and + behave just like the ! and they are not carried forth in the replacement in the position of $2.
8LWXpg
Posts: 41
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 24 Apr 2023, 09:42

@EFH52

The function clears input with trigger keys and nontext keys, so input it stored will only be words, which makes it impossible to match with white space charaters and word boundaries (or anything related to that), current workaround is creat two seperate RegExHotstring.

Code: Select all

; this one triggers with \W
RegExHotstring("(\d+)yr(\W)", "$1-year$2", "*")
; this one triggers with [\n\t ]
RegExHotstring("(\d+)yr", "$1-year")
if you really wants to be sepecific I will suggest using functions for better readablity:

Code: Select all

RegExHotstring("(\d+)yr(\W)", year, "*")
year(match) {
	switch match[2] {
		case "!", "?":
			Send(match[1] "-year" match[2])
		case "\", "/":
			Send(match[1] "-year")
		default:
			Send(match[1] "yr")
	}
}
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 24 Apr 2023, 09:47

Right. Exactly as was mentioned earlier in the thread. I should learn to read and retain better.
Thanks for the details and repeating yourself!
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 24 Apr 2023, 10:12

My previous message is still in the moderation queue, but the !, {, and } still won't carry over even when expanded as in the explicit function you kindly demonstrated. I modified it to include { and }, but even before that the ! wouldn't carry. I did attempt to escape the sequenced with "`!", "`{", and "`}" to no avail.
3yr! yields 3-year

Code: Select all

RegExHotstring("(\d+)yr(\W)", year, "*")
year(match) {
	switch match[2] {
		case "!", "?":
			Send(match[1] "-year" match[2])
		case "\", "/":
			Send(match[1] "-year")
		default:
			Send(match[1] "yr")
	}
}[\code]
8LWXpg
Posts: 41
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 25 Apr 2023, 10:11

@EFH52

I don't exactly know what problem you've occurred, it works as intended as I tested it on my mechine, even executing step by step in debug mode.

Maybe you're unfamilier with RegExMatchInfo it returns or Switch?
https://www.autohotkey.com/docs/v2/lib/RegExMatch.htm#MatchObject
https://www.autohotkey.com/docs/v2/lib/Switch.htm
Although it's not mentioned explicitily in the document, mutiple case values can be merged in to one and seperated by comma.

Code: Select all

RegExHotstring("(\d+)yr(\W)", year, "*")
year(match) {
	switch (match[2]) {
		case "-", "/", "\", "[", "]":
			Send(match[1] "-year" match[2])
		case "!", "{", "}", "+":
			Send(match[1] "-year")
		default:
			Send(match[1] "yr")
	}
}
and you only need to escape some special characters listed here (not include ":" and "{")
https://www.autohotkey.com/docs/v2/misc/EscapeChar.htm
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 25 Apr 2023, 12:24

We may be speaking to crossed ideas. I want match[2] on the end of everything. I am not getting it for !, {, and } in the position of match[2]. I am not concerned if it is with the use of a switch statement or not. I simply reposted the code you gifted me and was confused by the ! not finding its way into match[2]. I appreciate the links. I read about the match object yesterday and it certainly helped me follow your code better. I was spurred to do so by digging into your whole script.

I'll try again, it seems I can't say what I mean.

You provided me with this example:

Code: Select all

RegExHotstring("(\d+)yr(\W)", year, "*")
year(match) {
	switch match[2] {
		case "!", "?":
			Send(match[1] "-year" match[2])
		case "\", "/":
			Send(match[1] "-year")
		default:
			Send(match[1] "yr")
	}
}
I would expect 3yr! to expand to 3-year!, but I get just 3-year. Is your machine given different output?
8LWXpg
Posts: 41
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 26 Apr 2023, 00:46

@EFH52

I found the problem, it's because Send(), use SendText() instead.

I also changed one in source code
https://github.com/8LWXpg/RegExHotstring/releases/tag/v2.7
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 26 Apr 2023, 07:32

Good deal! Thanks. I was convinced I had messed up a setting and that it was a local issue. I'll start migrating all my shortcuts over now. This is a godsend for my v2 update!
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 26 Apr 2023, 08:18

With the script that I used in v1 I had created a RegEx Hotstring that provided the skeleton of a new entry and placed the cursor in position to take the new Hotstring as such:

Code: Select all

hotstrings("\bhtstr\s","hotstrings(""\b""tailchar,""``%`$`1```%""){left 17}")
With the 2.7 update here taking Send and making it SendText it invalidated the {left 17} call.
Though with the current ability to pass the action off to a function, we can return that ability in a simple rewrite.

Code: Select all

RegExHotstring("\bhtstr", builder)
builder(match) {
    Send("RegExHotstring(`"\b`",`"$1`"){left 7}")
}
This gives me the same result as I was accustomed to and provides significant enhancements over the product I was using in v1.
Thank you for sharing the script, insight, and guidance to make these solutions possible.
Now to fix it up such that it will give both the white-space trigger or the \W trigger versions for each Hotstring moving forward:

Code: Select all

RegExHotstring("\bhtstr", builder)
builder(match) {
    Send("RegExHotstring(`"\b`",`"`"){enter}")
    Send("RegExHotstring(`"\b(\W)`",`"$1`",`"*`"){up}{left 5}")
}
Yields:

Code: Select all

RegExHotstring("\b","")
RegExHotstring("\b(\W)","$1","*")
With the cursor at the end of the first \b ready for the new trigger to be entered.
8LWXpg
Posts: 41
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 27 Apr 2023, 06:43

@EFH52

maybe you mean this?

Code: Select all

RegExHotstring("htstr", builder)
builder(*) {
	RegExHotstring("", (*) => Send("{Enter}"))
	RegExHotstring("\W", (*) => Send("{Up}{Left 5}"), "*")
}
note that there's no need to specify "\b" because input only collects after it
and since you're not using RegEx in the first one, I think it's better to replace with a regular hotstring

I also found a bug with line like this

Code: Select all

RegExHotstring("\W", (*) => Send("{Up}{Left 5}"), "*")
and pushed a update
https://github.com/8LWXpg/RegExHotstring/releases/tag/v3.0
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 01 May 2023, 09:31

Code: Select all

    Send("RegExHotstring(`"\b`",`"`"){enter}")
    Send("RegExHotstring(`"\b(\W)`",`"$1`",`"*`"){up}{left 5}")
Was the intention. These stub out the entry of a new RegExHotstring.
So that I can use the stub to fill things out like this:

Code: Select all

RegExHotstring("\b(r|R)qd","$1equired")
RegExHotstring("\b(r|R)qd(\W)","$1equired$2","*")
And these use Regex.
so the {up}{left 5] just lands the cursor so that I can type in a new hotstring.

I'll kill all the \b's now that they are not required in this implementation!

Thanks for the update, I'll update my copy as well.
8LWXpg
Posts: 41
Joined: 26 May 2022, 03:55

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by 8LWXpg » 01 May 2023, 11:29

@EFH52

Simply add it within the functions. Once executed, the new RegExHotstring will be added.

Code: Select all

RegExHotstring("htstr", builder)
builder(*) {
	RegExHotstring("", (*) => Send("{Enter}"))
	RegExHotstring("\W", (*) => Send("{Up}{Left 5}"), "*")
}
This code creates two new RegExHotstrings when you type "htstr". The first one triggers Send("{Enter}") and the second one triggers Send("{Up}{Left 5}").

If you want to write function in one line, you need to put parentheses around them and use fat arrow syntax. Otherwise, they will be evaluated as their return values instead of functions.
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 01 May 2023, 11:35

Nevermind, we are talking at cross proposes and trying to accomplish two different things. I do need to learn the syntax with the fat arrows though.
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 02 May 2023, 09:24

It seems that this script is causing * on the number pad to type 8.
Please test. The problem goes away if I remove:

Code: Select all

#Include RegExHotstring.ahk
edit: seems + on the numpad comes out as =.
Further testing shows numpad enter fails as well.
EFH52
Posts: 16
Joined: 21 Mar 2016, 10:00

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by EFH52 » 03 May 2023, 13:18

Hello again!
CAPSLOCK fails to function now with this script included.
frabjous
Posts: 10
Joined: 30 Mar 2014, 16:44

Re: RegExHotstring() dynamic RegEx Hotstrings

Post by frabjous » 12 May 2023, 07:49

I would like to put a Unicode character in the replacement string, e.g. {U+2013} (an en dash). That works OK with a normal AHK hotstring but when using RegExHotstring you simply get the same characters i.e. {U+2013}
Is there a work-around?
Many thanks!
Post Reply

Return to “Scripts and Functions (v2)”