RegEx issue. Of course.... Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

RegEx issue. Of course....

08 Feb 2018, 19:09

I tried a bunch of googling and jeesweg's examples and just could not solve the GA RegEx. -.-
Im trying to extract any and all matches with RegExMatch for some anims. I want to modify the anims easy and create sequences so the easiest way i thought was to use variables with multiple entries. I did not actually start on it, trying to find the solution to separate stuff first.
So, anyone know the regex thing for extracting something like this:

I tried mutliple RegEx combos, this being the latest.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Now, i could use tons of loops that would match one by one and removing one entry at a time but i thought a regex match would be much more efficient because it already sets any match to an array that i can later parse. Any thoughts?
FanaticGuru
Posts: 1166
Joined: 30 Sep 2013, 22:25

Re: RegEx issue. Of course....

09 Feb 2018, 01:31

RegExMatch does not work the way you think it does.

It does not return MatchCount because it only ever matches once and it returns the position of that one match. Within that one match it might capture multiple subpatterns.

To find multiple matches of the needle pattern you have to use multiple RegExMatch, preferably in a loop of some sort.

Maybe this will help start the learning process.

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

Haystack = 
(Join`n
The first date is January 5, 2017
The second date is February 12, 2018
)

Needle := "m`aU)date is (.+) (\d+), (\d+)$"

X:=1
while (X := RegExMatch(Haystack, Needle, M, X+StrLen(M)))
MsgBox % "Month = " M1 "`nDay = " M2 "`nYear = " M3
The while code is optimized for compactness which makes it a little more cryptic but you don't really have to understand it completely to use it.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: RegEx issue. Of course....

09 Feb 2018, 04:02

Thanks for trying to explain but that didnt help me much. xD
I guessed i need a loop but perhaps i didnt give a proper example since i never got it to work so i did not have any loops.
My idea was (to summarize in theory):

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


So i want to turn this:

Code: [Select all] [Download] GeSHi © Codebox Plus

Sequence1=Action PlayOnce
Sequence2=Move X 500
Sequence3=Move Y 300
Sequence3=Action Finish

into this:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


I can track down a single occurance but there can be multiple of same type but with different instructions so i need to extract them in sequence.

At least thats how it looks in my head. Im trying to condense code and make it easier to overview and edit an animation.
Look at AnimSeq as an animation event that will do a particular thing and if you command a new AnimSeq, a new anim event takes place until finish is encountered.
Or to go the manual way and add every sequence manually in order with instructions that will eat a lot of lines.
I know about while but i need loop so can perhaps add more functionality. In while, its limited. Also, its easier to track. :)
Sorry for the wall post, trying to be as clear as possible and im still not good enough in english to do it in fewer lines i guess. -.-
FanaticGuru
Posts: 1166
Joined: 30 Sep 2013, 22:25

Re: RegEx issue. Of course....  Topic is solved

09 Feb 2018, 15:33

Here is a more specific example of what you seem to want.

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



If you don't need the commands broken up into the "Command" (ie Move) and the "Info" (ie X 500) then you can do it simpler without Regex.

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

AnimSeq=[Move X 500] [Move Y 300] [Action Pause 3000] [Move X 500] [Move Y 300] [Action Pause 3000] [Action StartAnim AnimSeq2]

Sequence := StrSplit(AnimSeq, "]", "[ ")
Sequence.Pop()
; Display Results
for Index, Command in Sequence
MsgBox % "Index = " Index "`nCommand = " Command


If you are not pulling the AnimSeq data from somewhere already in the format given and instead are actually entering this information then a format change makes it be put into an array directly.

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

AnimSeq :=["Move X 500","Move Y 300","Action Pause 3000","Move X 500","Move Y 300","Action Pause 3000","Action StartAnim AnimSeq2"]
; Display Results
for Index, Command in AnimSeq
MsgBox % "Index = " Index "`nCommand = " Command

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: RegEx issue. Of course....

09 Feb 2018, 15:42

Nice! 3 examples. Ill play around later and post which one works best for me. Maybe use all for different things. In any case, all 3 are awesome examples. Thanks!
User avatar
Masonjar13
Posts: 1389
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: RegEx issue. Of course....

09 Feb 2018, 15:50

Quick (possibly) helpful note: RegExReplace can be used here.

Code: [Select all] [Download] GeSHi © Codebox Plus

RegExReplace(str,regExpression,,cnt)
msgbox % cnt
FanaticGuru
Posts: 1166
Joined: 30 Sep 2013, 22:25

Re: RegEx issue. Of course....

09 Feb 2018, 16:09

Masonjar13 wrote:Quick (possibly) helpful note: RegExReplace can be used here.

Code: [Select all] [Download] GeSHi © Codebox Plus

RegExReplace(str,regExpression,,cnt)
msgbox % cnt
It is a good point that RegExReplace can be used to "count" the occurances of a pattern in a string. But as not to side track those with limited knowledge RegExReplace cannot capture patterns.

Which seems to be the confusion with the OP, in that RegExMatch will not find every occurance of a pattern the same way that RegExReplace does.

RegExMatch finds one occurance of a pattern and can return the pattern found. You have to do a separate RegExMatch for every occurance of a pattern in a string.

RegExReplace can find all occurances of a pattern and can change the pattern but does not return information from the pattern.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: RegEx issue. Of course....

09 Feb 2018, 16:32

Well, could RegExReplace use AnimSeq in a loop and set a loop index variable with first match and remove that match from AnimSeq?
Like
Loop
{
FoundString := command%a_index% := regexre(AnimSeq, exp)
StringReplace, AnimSeq, FoundString,, 0
; here would be FoundString reformated to use the end value
}
Thats how I guess RegExReplace could be used?
Now really interested how far I missed. :D
FanaticGuru
Posts: 1166
Joined: 30 Sep 2013, 22:25

Re: RegEx issue. Of course....

09 Feb 2018, 17:18

theimmersion wrote:Well, could RegExReplace use AnimSeq in a loop and set a loop index variable with first match and remove that match from AnimSeq?
Like
Loop
{
FoundString := command%a_index% := regexre(AnimSeq, exp)
StringReplace, AnimSeq, FoundString,, 0
; here would be FoundString reformated to use the end value
}
Thats how I guess RegExReplace could be used?
Now really interested how far I missed. :D
Remember where I said, "But as not to side track those with limited knowledge RegExReplace cannot capture patterns."

This is not the droid you are looking for.

There are Help documents that contain lots of information.
StringReplace < click for documents.

No where in there is anything about "FoundString". Every parameter position designated by commas after a command has a very specific and preset meaning and usage. Wanting them and using them as something different will not work.

Forget all about RegExReplace as nothing in your post appears to have to do with replacing anything in a string (put stuff into a string). You are attempting to match things in a string (get stuff out of a string).

To get the code you want I would advise studying the three examples give as they demonstrate a lot of very useful techniques about RegExMatch, objects, for-loop, etc. RegEx is a powerful sub-discipline but objects are what takes most people to the next level. Sequences of data, that is what arrays were made for which is one of the simplest objects. Probably a lot of what you want to accomplish with your overall script could be made easier by a solid understanding of objects. Objects are the core of most popular programming languages. Windows itself is very object oriented.

You are trying. That is the first step to succeeding.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
Masonjar13
Posts: 1389
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: RegEx issue. Of course....

09 Feb 2018, 17:29

Was the objective to get all the matches? There is a bit of a hackish way to do it only calling RegExReplace and RegExMatch once.

Code: [Select all] [Download] GeSHi © Codebox Plus

regExReplace(str,regExpression,,cnt)
loop % cnt
fregExpression.="(" . regExpression . ")" ; append expression "cnt" number of times, each acting as a subpattern
regExMatch(str,"O)" . fregExpression,strOut)

All matches would then be in strOut, accessed with strOut.value(2), strOut[2], or strOut.2, where 2 is the second match. This should technically be faster, since it's only calling each RegEx function once. You could also not use O), I just think it would be easier.
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: RegEx issue. Of course....

09 Feb 2018, 17:52

Masonjar13 wrote:Was the objective to get all the matches?

Uhm, if you mean in per [xxx] as independent entries, yes? Finding all matches is a broad question for this one. xDDDDD
Sorry for confustion. This is hard explaining sometimes. :oops:

I was just thinking in theory of how if at all possible how RegExReplace would be used.
But the task here was to accomplish to read one main variable that can have many different commands in any order and make a script do stuff based on them.
That is why it was kinda important to find a consistent and reliable way of getting each command within a single variable and separate them for further reformat.

Like, i could use any combination in any order of:

Code: [Select all] [Download] GeSHi © Codebox Plus

[Move X "some number"]
[Move Y "some number"]
[Rotate "some positive or negative number"]
[FrameCount "some number"]
[Action "some keyword action to be executed"]
etc...


inside a single variable like:
AnimationSequence:=
in any order based on my needs for some animation i want to make on screen and the script would act based on them in sequence (hence called a sequence).
Starting with the first [command] than [command] what ever they might be.

I can make a gdi+ image rotate then move, or move than do some action, like change the image to something else, add more options etc.
Look at the AnimationSequence as some form of animation matinee. I can have any number of sequences that hold any combo of move/rotate/change image combo instructions to execute.

Thank you both for those awesome informations. I think i can safely mark it solved.
Might i just point out this as also a very important info:
FanaticGuru wrote:To get the code you want I would advise studying the three examples give as they demonstrate a lot of very useful techniques about RegExMatch, objects, for-loop, etc. RegEx is a powerful sub-discipline but objects are what takes most people to the next level. Sequences of data, that is what arrays were made for which is one of the simplest objects. Probably a lot of what you want to accomplish with your overall script could be made easier by a solid understanding of objects. Objects are the core of most popular programming languages. Windows itself is very object oriented.

and kind of a pep talk. :salute: :superhappy:
Cheers guys! :D
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: RegEx issue. Of course....

10 Feb 2018, 08:47

It would be nice to get some feedback how to rename the topic to help find this easier and more pecise for what it solves. xD
Not a programmer so i have no clue how to call it. :oops:

I have made a test script demo to try out with comments and tried to change some stuff that would make it easier for me and hopefully for other newcomers as well.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Sorry if i over-explained some stuff.
Cheers :thumbup:
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: RegEx issue. Of course....

10 Feb 2018, 14:22

Masonjar13 wrote:This should technically be faster, since it's only calling each RegEx function once

It might, or might not, using muliple calls with simpler patterns can be faster than one with a complex pattern. It is case depenent. There was a thread which showed that something like regexreplace(str,"a|b|c") was slower than three calls regexreplace(str, "a"), ..., regexreplace(str, "c"), if I recall correctly.

Cheers.
User avatar
jeeswg
Posts: 4989
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RegEx issue. Of course....

25 Feb 2018, 15:28

- I've added a bit more to my 'GET ALL MATCHES' section here:
jeeswg's RegEx tutorial (RegExMatch, RegExReplace) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28031
- For the original question, here's a RegExMatch with while loop example, but also a StrSplit example. I would probably use StrSplit for something like this. Cheers.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Return to “Ask For Help”

Who is online

Users browsing this forum: 1100++ and 82 guests