Jump to content


Photo

RegEx Powered Dynamic Hotstrings


  • Please log in to reply
197 replies to this topic

#1 polyethene

polyethene

    Administrator

  • Administrators
  • 5473 posts

Posted 04 February 2007 - 05:59 PM

Dynamically add regular expression type hotstrings.

Example:
hotstrings("[color=brown]now#[/color]", "%A_Now%")
hotstrings("[color=brown](B|b)tw[/color]", "%$1%y the way")
hotstrings("[color=brown](\d+)\/(\d+)%[/color]", "percent") ; try 4/50%
Return

percent:
p := Round($1 / $2 * 100) 
Send, %p%`%
Return
Download

#2 hughman

hughman
  • Members
  • 189 posts

Posted 21 February 2007 - 12:10 PM

Dynamically add regular expression type hotstrings.


Thinks.I'll have a try.

#3 rjwilmsi

rjwilmsi
  • Members
  • 30 posts

Posted 28 February 2007 - 06:59 PM

Hmm, this seems interesting, but I can't get it to work.
I've pasted the function and examples into my autohotkey.ini file and reloaded it. I then expected that if I typed Btw in notepad it would be converted to By the way, and if I typed btw it would be converted to by the way. Am I totally missing the point? Does this script not effectively add regex support to hotstrings?

#4 polyethene

polyethene

    Administrator

  • Administrators
  • 5473 posts

Posted 04 March 2007 - 03:05 PM

Does this script not effectively add regex support to hotstrings?

It does, that was probably a bad example. Try hs("gr[ea]y", "{#}CCC") - it replaces 'grey' or 'gray' with its hex colour code.

#5 ahklerner

ahklerner
  • Members
  • 1382 posts

Posted 05 April 2007 - 11:52 PM

Used it here
http://www.autohotke...pic.php?t=18110

#6 talkasab

talkasab
  • Members
  • 2 posts

Posted 17 July 2007 - 01:09 PM

I like the idea of the dynamic hotstrings and I like the idea of regular expressions in them. But, as other people have described, I'd like to find a way to make dynamic hotstrings that behave more like regular, default hotstrings in that they wait for an non-word character before firing the replacement text.

For example, when defined using the hs() function above, the "btw" entry will expand to "by the way" without waiting for a space or comma afterward. (Which makes it impossible to define multiple hotstrings that use the same root.)

For example, I'd "lb" to expand to "pound", but "lbrt" to expand to liberate, and I haven't figured out yet how to do that using the hs() function above.

(The reason I don't want to use the #Hostring directive or syntax is that I'd like these hotstrings to be dynamically created as part of a compiled script.)

Thanks,
T

#7 polyethene

polyethene

    Administrator

  • Administrators
  • 5473 posts

Posted 17 July 2007 - 01:24 PM

That is an important feature that's currently lacking. I've been meaning to update this script for a long time. I'll try work on it soon and release version 2.

#8 engunneer

engunneer
  • Fellows
  • 9162 posts

Posted 17 July 2007 - 02:33 PM

please try
hs("(B|b)tw\W", "%$1%y the way")


#9 talkasab

talkasab
  • Members
  • 2 posts

Posted 17 July 2007 - 04:07 PM

please try

hs("(B|b)tw\W", "%$1%y the way")

Does that work for you? Because that doesn't get expanded at all for me. (FWIW, I also thought about using regular expressions to try to test for end-of-word characters, but couldn't get it to work.)

T

#10 polyethene

polyethene

    Administrator

  • Administrators
  • 5473 posts

Posted 17 July 2007 - 04:11 PM

That would work if you typed something like 'btw.' or 'btw!', but these aren't nice solutions so I'll continue working on an update.

#11 engunneer

engunneer
  • Fellows
  • 9162 posts

Posted 17 July 2007 - 04:36 PM

I didn't have a chance to try it. I also didn't have time to look up the regex, maybe \s was the right one?

#12 rjwilmsi

rjwilmsi
  • Members
  • 30 posts

Posted 17 December 2007 - 07:15 PM

Hi all,
I've been using this for a while now but have got stuck by one issue:

say i define an entry like this (I have this problem with other examples):

hs("ann(a|e)" , "Ann%$1%")

now this correctly changes:
anna -> Anna
anne -> Anne

however, spaces don't seem to be counted, so these are also incorrectly changed;
ann a -> aAnna
ann e -> aAnne

So it would seem that spaces are ignored by the regex matching? Am I right and can anybody tell me how to fix it?

Thanks

#13 rjwilmsi

rjwilmsi
  • Members
  • 30 posts

Posted 21 December 2007 - 08:11 AM

Please, anybody? I haven't yet resolved this.

#14 tonne

tonne
  • Members
  • 1654 posts

Posted 21 December 2007 - 08:49 AM

Try this:

hs(c, a = "") {
	static d, b, q
	global $
	v = `n,€,~$,Numpad,BS[color=red],Space[/color]
	StringSplit, v, v, `,
	If !d {
		Loop, 104
			Hotkey, % Chr(42 * !!RegExMatch(t := Chr(32 + (x := A_Index))
				, "[a-z]")) . v3 . (x > 94 ? v4 . x - 95 : t), _hs
		Hotkey, %v3%%v5%, _hs
		[color=red]d := 1[/color]
	}
	If a
		Return, !!b .= c . v2 . a . v1
	If c = %v3%%v5%
		Return, !!q := SubStr(q, 1, -1)
[color=red]	If c = %v3%%v6%
		Return, !!q := SubStr(q, 1, -1)[/color]
	If GetKeyState("Shift", "P") or GetKeyState("Capslock", "T")
		StringUpper, c, c
	Else StringLower, c, c
	q .= SubStr(c, StrLen(v3) + !!InStr(c, v4) * StrLen(v4) + 1 + !!InStr(c, "*"))
	Loop, Parse, b, %v1%
	{
		If (c := RegExMatch(q, SubStr(t := A_LoopField, 1
			, InStr(t, v2) - 1) . "$", $)) and t {
			f += RegExMatch(t, ".+" . v2 . "(.+)", l)
			SendInput, % "{BS " . StrLen($) . "}"
			Transform, l, Deref, %l1%
			If IsLabel(l)
				GoSub, %l%
			Else SendInput, %l%
		}
	}
	If f
		q =
}

[color=red]~$space:: [/color]
_hs:
hs(A_ThisHotkey)
Return


#15 rjwilmsi

rjwilmsi
  • Members
  • 30 posts

Posted 21 December 2007 - 06:23 PM

Thanks, that's perfect!

For others who may be interested, it's possible to exclude returns and tabs in a similar fashion:

hs(c, a = "") {
   static d, b, q
   global $
   v = `n,€,~$,Numpad,BS,Space[color=red],Tab,Return[/color]
   StringSplit, v, v, `,
   If !d {
      Loop, 104
         Hotkey, % Chr(42 * !!RegExMatch(t := Chr(32 + (x := A_Index))
            , "[a-z]")) . v3 . (x > 94 ? v4 . x - 95 : t), _hs
      Hotkey, %v3%%v5%, _hs
      d := 1
   }
   If a
      Return, !!b .= c . v2 . a . v1
   If c = %v3%%v5%
      Return, !!q := SubStr(q, 1, -1)
   If c = %v3%%v6%
      Return, !!q := SubStr(q, 1, -1)
  [color=red] If c = %v3%%v7%
      Return, !!q := SubStr(q, 1, -1)
   If c = %v3%%v8%
      Return, !!q := SubStr(q, 1, -1)[/color]
   If GetKeyState("Shift", "P") or GetKeyState("Capslock", "T")
      StringUpper, c, c
   Else StringLower, c, c
   q .= SubStr(c, StrLen(v3) + !!InStr(c, v4) * StrLen(v4) + 1 + !!InStr(c, "*"))
   Loop, Parse, b, %v1%
   {
      If (c := RegExMatch(q, SubStr(t := A_LoopField, 1
         , InStr(t, v2) - 1) . "$", $)) and t {
         f += RegExMatch(t, ".+" . v2 . "(.+)", l)
         SendInput, % "{BS " . StrLen($) . "}"
         Transform, l, Deref, %l1%
         If IsLabel(l)
            GoSub, %l%
         Else SendInput, %l%
      }
   }
   If f
      q =
}

~$space::
[color=red]~$return::
~$tab::[/color]
_hs:
hs(A_ThisHotkey)
Return