Page 1 of 1

[Solved]Help with a RegEx?

Posted: 08 Aug 2014, 05:22
by ozzii
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 !?

Re: Help with a RegEx?

Posted: 08 Aug 2014, 05:41
by Guest
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")

Re: Help with a RegEx?

Posted: 08 Aug 2014, 05:49
by jNizM
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]

Re: Help with a RegEx?

Posted: 08 Aug 2014, 06:26
by ozzii
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 ;)

Re: Help with a RegEx?

Posted: 08 Aug 2014, 06:34
by jNizM
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]

Re: Help with a RegEx?

Posted: 08 Aug 2014, 06:45
by ozzii
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?

Re: Help with a RegEx?

Posted: 08 Aug 2014, 08:04
by ahcahc

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

Re: Help with a RegEx?

Posted: 08 Aug 2014, 08:16
by jNizM
InStr() is the replacement for IfInString
Same for every Ifxxxxxx

AutoHotkey v2 removed commands.
v2-changes

Re: Help with a RegEx?

Posted: 08 Aug 2014, 08:20
by ozzii
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.

Re: Help with a RegEx?

Posted: 08 Aug 2014, 08:29
by jNizM
Really?
Image

Image

Re: Help with a RegEx?

Posted: 11 Aug 2014, 02:36
by ozzii
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.