[Solved]Help with a RegEx?

Get help with using AutoHotkey and its commands and hotkeys
ozzii
Posts: 343
Joined: 30 Oct 2013, 06:04

[Solved]Help with a RegEx?

08 Aug 2014, 05:22

Hi,
I have this:
71: ,'first part','second part','wanted block','last part','another wanted block'

If I search for wanted I would like to have as a result:
71: ,'wanted block','another wanted block'

Info:
-the searched word can be anywhere on the line
-the searched word can be partial so if i search for want i found wanted block and another wanted block

Am I clear enough, I'm not sure !?
Last edited by ozzii on 11 Aug 2014, 02:36, edited 1 time in total.
Guest

Re: Help with a RegEx?

08 Aug 2014, 05:41

Perhaps

line1='first part','second part','wanted block','last part','another wanted block'
line2='wanting to have this','second part','third','last part','another want'
line3='want','more want'

MsgBox % RegExReplace(line1,"iUms).*('[^',]*want[^',]*').*('[^',]*want[^',]*')","$1,$2")
MsgBox % RegExReplace(line2,"iUms).*('[^',]*want[^',]*').*('[^',]*want[^',]*')","$1,$2")
MsgBox % RegExReplace(line3,"iUms).*('[^',]*want[^',]*').*('[^',]*want[^',]*')","$1,$2")
User avatar
jNizM
Posts: 2613
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Help with a RegEx?

08 Aug 2014, 05:49

with loop parse to array

Code: Select all

string := "71: ,'first part','second part','wanted block','last part','another wanted block'"
arrSearch := []

loop, parse, string, `,
    arrSearch[A_Index] := A_LoopField

MsgBox % arrSearch[1] "`n" arrSearch[2] "`n" arrSearch[3] "`n" arrSearch[4] "`n" arrSearch[5] "`n" arrSearch[6]
next step only 4 & 6

Code: Select all

string := "71: ,'first part','second part','wanted block','last part','another wanted block'"
arrSearch := []

loop, parse, string, `,
    if (A_Index = 4) || (A_Index = 6)
        arrSearch[A_Index] := A_LoopField

MsgBox % arrSearch[1] "`n" arrSearch[2] "`n" arrSearch[3] "`n" arrSearch[4] "`n" arrSearch[5] "`n" arrSearch[6]
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
ozzii
Posts: 343
Joined: 30 Oct 2013, 06:04

Re: Help with a RegEx?

08 Aug 2014, 06:26

Guest wrote:Perhaps

line1='first part','second part','wanted block','last part','another wanted block'
line2='wanting to have this','second part','third','last part','another want'
line3='want','more want'

MsgBox % RegExReplace(line1,"iUms).*('[^',]*want[^',]*').*('[^',]*want[^',]*')","$1,$2")
MsgBox % RegExReplace(line2,"iUms).*('[^',]*want[^',]*').*('[^',]*want[^',]*')","$1,$2")
MsgBox % RegExReplace(line3,"iUms).*('[^',]*want[^',]*').*('[^',]*want[^',]*')","$1,$2")
Working but just with 2 found. If I have 3 or 4 or 1?

jNizM wrote:with loop parse to array

Code: Select all

string := "71: ,'first part','second part','wanted block','last part','another wanted block'"
arrSearch := []

loop, parse, string, `,
    arrSearch[A_Index] := A_LoopField

MsgBox % arrSearch[1] "`n" arrSearch[2] "`n" arrSearch[3] "`n" arrSearch[4] "`n" arrSearch[5] "`n" arrSearch[6]
next step only 4 & 6

Code: Select all

string := "71: ,'first part','second part','wanted block','last part','another wanted block'"
arrSearch := []

loop, parse, string, `,
    if (A_Index = 4) || (A_Index = 6)
        arrSearch[A_Index] := A_LoopField

MsgBox % arrSearch[1] "`n" arrSearch[2] "`n" arrSearch[3] "`n" arrSearch[4] "`n" arrSearch[5] "`n" arrSearch[6]
But here nowhere a search from my want keyword.
And the wanted keyword is not always on the index 4 or 6 ;)
User avatar
jNizM
Posts: 2613
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Help with a RegEx?

08 Aug 2014, 06:34

than you can change if (A_Index) to if InStr()

Code: Select all

string := "71: ,'first part','second part','wanted block','last part','another wanted block'"
arrSearch := []
i := 1

loop, parse, string, `,
    if (InStr(A_LoopField, "want"))
        arrSearch[i++] := A_LoopField

MsgBox % arrSearch[1] "`n" arrSearch[2] "`n" arrSearch[3] "`n" arrSearch[4] "`n" arrSearch[5] "`n" arrSearch[6]
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
ozzii
Posts: 343
Joined: 30 Oct 2013, 06:04

Re: Help with a RegEx?

08 Aug 2014, 06:45

I came up with this I don't really need an array:

Code: Select all

string := "71: ,'first part','second part','wanted block','last part','another wanted block'"
loop, parse, string, `,
{
	if (InStr(A_LoopField, "want"))
		Var .= "," . A_LoopField
}
msgbox % Var
Now I just have to make it into a RegExMatch()

Thanks both for the help. Easy but I don't know I didn't found alone a way to do it :thumbdown:

One question: is there any difference between InStr() and IfInString ?
Maybe one is faster than the other or it's the same?
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Help with a RegEx?

08 Aug 2014, 08:04

Code: Select all

string := "71: ,'first part','second part','wanted block','last part','another wanted block'"

string2:= "71: "
startp=1
Loop
{
foundp := RegExMatch(string, "'[^']*?want.*?'", match,startp)
if match is space
	break
string2 := string2 "`," match
startp := foundp+1
}
MsgBox % string2
User avatar
jNizM
Posts: 2613
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Help with a RegEx?

08 Aug 2014, 08:16

InStr() is the replacement for IfInString
Same for every Ifxxxxxx

AutoHotkey v2 removed commands.
v2-changes
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
ozzii
Posts: 343
Joined: 30 Oct 2013, 06:04

Re: Help with a RegEx?

08 Aug 2014, 08:20

This is it ahcahc.
I was on that way but you were faster.
Thanks all.

OK jNizM.
I will try to remember this and to use the new one.
I think that it will be better to have this kind of information in the help.
Just under the IfInString put a link to the new function (just my opinion).
I am still under V1 but the latest one.
User avatar
jNizM
Posts: 2613
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Help with a RegEx?

08 Aug 2014, 08:29

Really?
Image

Image
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
ozzii
Posts: 343
Joined: 30 Oct 2013, 06:04

Re: Help with a RegEx?

11 Aug 2014, 02:36

I didn't explain myself well. (sorry my English is medium)

I wanted to say that it's maybe better to have first the new function and after the old one.
For example in you second snapshot to have on the first line (before the explanation "Checks if..) a link/redirection for the new function and a note that say that it's better to use the new function.
Better if the old one will be outdated and not used any more.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler and 267 guests