Sign in to follow this  
Followers 0
cag8f

Matching array values in StringRegExp.

8 posts in this topic

Say I have 100 names stored in an array, and I want to search a large text file line by line for one of those names in the line, and perform operations if one is found. The line will always have the same format. Is there any way to do that without a loop? i.e. something like:

If StringRegExp($line, "text" & <any name in the array> & "more text") Then

Do stuff

EndIf

Does something like this syntax exist? Or do I need to somehow loop through the array to accomplish what I want? Now that I think about it, looping wouldn't be too tough. But for future reference, can I accomplish without looping?

Share this post


Link to post
Share on other sites



Something like this maybe.

I'm not the best with Reg's but something like this should work.

There is a loop in there but still if your looking for a starting point.

Dim $names[4] = ["Sue","Steve", "Stuart","Sally"]

$file = FileOpen("t.txt")

while 1
    $line = FileReadLine($file)
    If @error then ExitLoop

    if StringRegExp($line, CompilePattern($names)) == 1 Then
        ConsoleWrite($line & " : Match" & @CRLF)
    EndIf

WEnd


Func CompilePattern($arr)
    Dim $result
    For $name in $arr
        $result &= $name & "|"
    Next
    $result = StringTrimRight($result, 1)

    Return "(.*)(?i:" & $result & ")(.*)"
EndFunc

My t.txt contained

Line1 has Sue
Line2 has noone
line3 has sally with lowercase
line4 has noone
line5 has noone
line6 has stuart and steve

They call me MrRegExpMan

Share this post


Link to post
Share on other sites

without a loop?

:x

What do you have against loops? :P


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Loops killed my father. Well hurt him badly. OKhurthisfeelingsstill.

Thanks for the sample script. I'm a noob though and have never seen a loop in the form:

For $name in $array

Next

What is that doing exactly? I assume it is looping over the array. $name is one of the entries in the array? The loop starts at that name (the first instance of that name?) and works its way up the array?

I'll end up using a loop, since even some built in function will probably utilize a loop in some fashion.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Yes,

$name will be one single element of an array. Then, Next happens. So $name becomes the $next element in the array.

They are also referred to as a Foreach loop in some languages.

ForEach $name in $names - Sounds a little more like english and more understandable. When i was learning i always thought of it like this.

Edited by Steveiwonder

They call me MrRegExpMan

Share this post


Link to post
Share on other sites

OK I see it in the help file now. The For...In...Next loop. Overlooked it earlier, thanks.

Share this post


Link to post
Share on other sites

Loops killed my father. Well hurt him badly. OKhurthisfeelingsstill.

lol.

He probably hurt his fingers by not using enuff loops. :x


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0