NeedleRegEx Variable help

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newpie
Posts: 37
Joined: 01 Jun 2014, 07:50

NeedleRegEx Variable help

27 Jul 2015, 23:12

Hello, I been experimenting with regex and had a question related to variables.

The below code works fine, if you replace the Haystack with the appropriate Message,

Code: Select all

MessageA = [7/4/2015 10:37:06 AM] Fred: Hello Friend 
MessageB = [7/4/2015 10:37:06 AM] Fred: Hello Bob
MessageC = [7/27/2015 10:00:33 PM] Sam (To Bob): What are you doing?

RegExMatch(MessageC, "\s(Friend|Bob)\D?|\s\(To Bob\):",match)
Msgbox, %match%
return
But when I try to make it more dynamic I can't get any results, is it possible to put a variable in regex above. Here is what I tried without success

Code: Select all

MessageA = [7/4/2015 10:37:06 AM] Fred: Hello Friend 
MessageB = [7/4/2015 10:37:06 AM] Fred: Hello Bob
MessageC = [7/27/2015 10:00:33 PM] Sam (To Bob): What are you doing?

Name = Bob
OtherName = Friend
RegExMatch(MessageC, "\s(%OtherName%|%Name%)\D?|\s\(To %Name%\):",match)
Msgbox, %match%
return
Thanks for any help.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: NeedleRegEx Variable help

28 Jul 2015, 00:41

RegEx is a function no need for % signs and variables are not enclosed in quotes

Code: Select all

RegExMatch(MessageC, "\s(" OtherName "|" Name ")\D?|\s\(To " Name "\):",match)
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: NeedleRegEx Variable help

28 Jul 2015, 09:58

You could potentially simplify your regex if your haystack is going to be that consistent:

Code: Select all

Message :=	["[7/4/2015 10:37:06 AM] Fred: Hello Friend"
		 ,"[7/4/2015 10:37:06 AM] Fred: Hello Bob"
		 ,"[7/27/2015 10:00:33 PM] Sam (To Bob): What are you doing?"]

For i, msg in Message
{
	RegExMatch(msg, "\]\s\K[^:]+:",match)
	Msgbox, %match%
}
return
newpie
Posts: 37
Joined: 01 Jun 2014, 07:50

Re: NeedleRegEx Variable help

28 Jul 2015, 17:52

Thanks for your help and time with this one guys.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Google [Bot] and 286 guests