[SOLVED] Search for string and extract them

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Peace1
Posts: 41
Joined: 17 Mar 2015, 14:19
Location: 127.0.0.1

[SOLVED] Search for string and extract them

17 Mar 2015, 15:12

Hi all,

Well, I search in the forum before posting but i didn't find any good solution, i'm not a beginner but not an expert in AHK coding and i need your help please if it's possible !

In my code i use an function to read the last 20 lines of the log and I need to extract the strings before and after the => symbole and i can't manage it to work good. Because the last line is always blank and there is space blank before the string, after the symbole etc...

The goal is to detect the last => in the log file and extract the strings before an after, in this case is :

%DesiredString1% = Reload the program
%DesiredString2% = The program was reloaded

Code: Select all

18:03:35:INFO:: = [0]  action planned >>>>>>>
18:03:35:INFO:: = [0]  Execute task1 :this is a test[2/2]
18:03:35:INFO:: = [0]  	Launch this program now => The program was launched
18:03:35:INFO:: = [0]  action paused, click [Continue] to resume, or [Reload] to reload.
18:03:44:INFO:: = [0]  action resumed.
18:03:49:INFO:: = [0]  no action rules matched.
18:03:49:INFO:: = [0]  action planned >>>>>>>
18:03:49:INFO:: = [0]  Execute task2 :this is a test[3/4]
18:03:49:INFO:: = [0]  	Reload the program => The program was reloaded
18:03:49:INFO:: = [0]  action paused, click [Continue] to resume, or [Reload] to reload.
18:04:02:INFO:: = [0]  action resumed.

If someone with skills can give a hand i will apreciate it please !

Is my first post here and i hope that can help someone else too :)

Best regards.
Last edited by Peace1 on 18 Mar 2015, 11:10, edited 1 time in total.
Guest

Re: [HELP] Search for string and extract them

17 Mar 2015, 15:27

There are many ways to do it I suppose, a simple parsing trick will find the last line very quickly as you only have 20 lines or so - after that you can use RegExReplace to get the two parts, I used StrSplit here to process it directly into a simple Array (DesiredString)

Code: Select all

log=
(join`n
18:03:35:INFO:: = [0]  action planned >>>>>>>
18:03:35:INFO:: = [0]  Execute task1 :this is a test[2/2]
18:03:35:INFO:: = [0]   Launch this program now => The program was launched
18:03:35:INFO:: = [0]  action paused, click [Continue] to resume, or [Reload] to reload.
18:03:44:INFO:: = [0]  action resumed.
18:03:49:INFO:: = [0]  no action rules matched.
18:03:49:INFO:: = [0]  action planned >>>>>>>
18:03:49:INFO:: = [0]  Execute task2 :this is a test[3/4]
18:03:49:INFO:: = [0]   Reload the program => The program was reloaded
18:03:49:INFO:: = [0]  action paused, click [Continue] to resume, or [Reload] to reload.
18:04:02:INFO:: = [0]  action resumed.
)

Loop, parse, log, `n, `r
	If InStr(A_LoopField, "=>")
		lastline:=A_LoopField

MsgBox % lastline ; just checking as test

DesiredString:=StrSplit(RegExReplace(lastline,"^.*\]\s*(.*)=>(.*)","$1|$2"),"|")

MsgBox % DesiredString[1] ":" DesiredString[2]
User avatar
Peace1
Posts: 41
Joined: 17 Mar 2015, 14:19
Location: 127.0.0.1

Re: [HELP] Search for string and extract them

17 Mar 2015, 16:06

Hi !

Thank you for the quick reply and for the help, I appreciate it.

ATM I'm not front of my computer (I reply you from my smartphone) so I will try your code when I back to home and give you the feedback.

Just for my brain can you explain me what does exactly this command (there are a lot of symbols) :

DesiredString:=StrSplit(RegExReplace(lastline,"^.*\]\s*(.*)=>(.*)","$1|$2"),"|")

Best regards and thank you in advance.
Guest

Re: [HELP] Search for string and extract them

18 Mar 2015, 02:22

the RegExReplace(lastline,"^.*\]\s*(.*)=>(.*)","$1|$2")

translates this line

18:03:49:INFO:: = [0] Reload the program => The program was reloaded

into

Reload the program | The program was reloaded

^.*\] = start of line until a ]
\s* = white space (spaces, tabs)
(.*)=> = all text until =>, remind it as $1
(.*) = the rest, remind it as $2

As it is a function you can pass on the result into another function,
here StrSplit which uses the | char to split the string in two and
store the result in a DesiredString

so the RegExReplace is executed first, the result is processed in StrSplit and the result of that is stored in DesiredString
just me
Posts: 9490
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [HELP] Search for string and extract them

18 Mar 2015, 04:01

Without a RegEx:

Code: Select all

#NoEnv
Log =
(join`r`n
18:03:35:INFO:: = [0]  action planned >>>>>>>
18:03:35:INFO:: = [0]  Execute task1 :this is a test[2/2]
18:03:35:INFO:: = [0]   Launch this program now => The program was launched
18:03:35:INFO:: = [0]  action paused, click [Continue] to resume, or [Reload] to reload.
18:03:44:INFO:: = [0]  action resumed.
18:03:49:INFO:: = [0]  no action rules matched.
18:03:49:INFO:: = [0]  action planned >>>>>>>
18:03:49:INFO:: = [0]  Execute task2 :this is a test[3/4]
18:03:49:INFO:: = [0]   Reload the program => The program was reloaded
18:03:49:INFO:: = [0]  action paused, click [Continue] to resume, or [Reload] to reload.
18:04:02:INFO:: = [0]  action resumed.
)
Loop, Parse, Log, `n, `r
   If InStr(A_LoopField, "=>")
      LastLineOfInterest := A_LoopField
MsgBox, 0, LastLineOfInterest, %LastLineOfInterest%
DesiredStrings := StrSplit(SubStr(LastLineOfInterest, InStr(LastLineOfInterest, "]") + 1), "=>", " `t")
; If you don't want to use array syntax later on:
Task := DesiredStrings[1]
Result := DesiredStrings[2]
MsgBox, 0, Result (Variables), %Task%<>%Result%
; Else:
MsgBox, 0, Result (Array), % DesiredStrings[1] "<>" DesiredStrings[2]
User avatar
Peace1
Posts: 41
Joined: 17 Mar 2015, 14:19
Location: 127.0.0.1

Re: [HELP] Search for string and extract them

18 Mar 2015, 05:54

Invité wrote:the RegExReplace(lastline,"^.*\]\s*(.*)=>(.*)","$1|$2")

translates this line

18:03:49:INFO:: = [0] Reload the program => The program was reloaded

into

Reload the program | The program was reloaded

^.*\] = start of line until a ]
\s* = white space (spaces, tabs)
(.*)=> = all text until =>, remind it as $1
(.*) = the rest, remind it as $2

As it is a function you can pass on the result into another function,
here StrSplit which uses the | char to split the string in two and
store the result in a DesiredString

so the RegExReplace is executed first, the result is processed in StrSplit and the result of that is stored in DesiredString
Hi mate !

Thank you for all your remarks that helped me a lot to understand how that work !

Now I can adjust the search because it still blank space before/after the => I just changed the code by :

Code: Select all

DesiredString := StrSplit(RegExReplace(lastline,"^.*\]\s*(.*) => (.*)","$1|$2"),"|")
Thank you very much for the quick help and for your kindness !

Best regards.
just me wrote:Without a RegEx:

Code: Select all

#NoEnv
Log =
(join`r`n
18:03:35:INFO:: = [0]  action planned >>>>>>>
18:03:35:INFO:: = [0]  Execute task1 :this is a test[2/2]
18:03:35:INFO:: = [0]   Launch this program now => The program was launched
18:03:35:INFO:: = [0]  action paused, click [Continue] to resume, or [Reload] to reload.
18:03:44:INFO:: = [0]  action resumed.
18:03:49:INFO:: = [0]  no action rules matched.
18:03:49:INFO:: = [0]  action planned >>>>>>>
18:03:49:INFO:: = [0]  Execute task2 :this is a test[3/4]
18:03:49:INFO:: = [0]   Reload the program => The program was reloaded
18:03:49:INFO:: = [0]  action paused, click [Continue] to resume, or [Reload] to reload.
18:04:02:INFO:: = [0]  action resumed.
)
Loop, Parse, Log, `n, `r
   If InStr(A_LoopField, "=>")
      LastLineOfInterest := A_LoopField
MsgBox, 0, LastLineOfInterest, %LastLineOfInterest%
DesiredStrings := StrSplit(SubStr(LastLineOfInterest, InStr(LastLineOfInterest, "]") + 1), "=>", " `t")
; If you don't want to use array syntax later on:
Task := DesiredStrings[1]
Result := DesiredStrings[2]
MsgBox, 0, Result (Variables), %Task%<>%Result%
; Else:
MsgBox, 0, Result (Array), % DesiredStrings[1] "<>" DesiredStrings[2]
Hi just me,

Thank you too for helping me in this little issue, I keep your method in case I need it one day !

Best regards.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS, GEOVAN and 156 guests