Sign in to follow this  
Followers 0
Nutster

Regular Expression Testing

139 posts in this topic

Written, tested and submitted to Jon.

<{POST_SNAPBACK}>

Thank you, my (beta)scripts broke when I upgraded to the final release.

I hope there will be a new beta release soon... as soon as Jon stopped lurking on his well earned cigar en nipping the cognac he got from the release party :lmao:

Share this post


Link to post
Share on other sites



Thank you, my (beta)scripts broke when I upgraded to the final release.

I hope there will be a new beta release soon... as soon as Jon stopped lurking on his well earned cigar en nipping the cognac he got from the release party :lmao:

Just download the 3.1.0.14 version from the beta directory. It's the same thing with the reg exp functions.

Share this post


Link to post
Share on other sites

Just download the 3.1.0.14 version from the beta directory.  It's the same thing with the reg exp functions.

ehm, sorry, this one?:

autoit-v3.1.0.exe 07-Feb-2005 05:59 1.7M

from

http://www.autoitscript.com/autoit3/files/beta/autoit/

I have a previous beta, so it's not that important

Share this post


Link to post
Share on other sites

Where's some documentation on this?

I've never used RegEx before and I had a friend, practically, do this for me.

$String = "200% defense; 200% damage; +4 Light radius; +1 all skills; +5 dex; +14 mana"
$String = StringRegExp( $String, "/\b([0-9]+)\% damage.*\+([0-9]+) all skills.*\+([0-9]+) mana.*/" )

MsgBox("","", $String )

That should work, no? String returns 3... even when I try to fool it.


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Where's some documentation on this?

I've never used RegEx before and I had a friend, practically, do this for me.

$String = "200% defense; 200% damage; +4 Light radius; +1 all skills; +5 dex; +14 mana"
$String = StringRegExp( $String, "/\b([0-9]+)\% damage.*\+([0-9]+) all skills.*\+([0-9]+) mana.*/" )

MsgBox("","", $String )

That should work, no?  String returns 3... even when I try to fool it.

<{POST_SNAPBACK}>

You're using Perl styled regular expressions, with the / on either end, this function uses more Posix based (from what I understand). The "3" that the function is returning is part of an error code telling you where the problem in your pattern is (character 3), if you have a look at @error it will probably not be 0. Also, if you want to have the contents of your groups returned (the parts in brackets) then you need to have a 3rd flag of the function set to 1 or 3. Then the return value will be an array. And I'm pretty sure the documentation still exists in the .14 beta.

Share this post


Link to post
Share on other sites

#126 ·  Posted (edited)

I cannot find it, where would the documentation be located?

Thanks, I tried it without the /'s, I'll try all the flags and fudge :lmao:

[EDIT]

$String = "200% defense 200% damage +4 Light radius +1 all skills +5 dex +14 mana"
$RegExp = StringRegExp( $String, "([0-9]+\% damage).*\+([0-9]+ all skills).*\+([0-9]+ mana).*" )

MsgBox("","", $RegExp )

Works on this site: http://www.quanetic.com/regex.php

using posix and everything... it returns '1' now, no matter what.

Edited by Insolence

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Make sure you're checking @error as well. With your current expression it's returning 2.

Lemme see if I can code something up that works, but unfortunately I'm flying blind as I just realized that I can't find any documentation on the function either!

Also unfortunately, I'm not totally sure what it is you want here. But here's the best I could figure out.

$String = "200% defense 200% damage +4 Light radius +1 all skills +5 dex +14 mana"
$RegExp = StringRegExp($String, "([0-9]+% damage).*?\+([0-9]+ all skills).*?\+([0-9]+ mana).*", 1)

For $i = 0 to UBound($RegExp) - 1
     MsgBox("","", $RegExp[$i] )
Next

The ? you see after the .* is telling it to be "ungreedy".

This is because if you allow it to go unchecked, then .* will match as much as possible. So if you have this expression for example:

String: "<Hello> <World>"

RegExp: "<(.*)>"

It's going to return "Hello> <World"

See where I'm going? It matched too much. But if your RegExp was: "<(.*?)>"

Then it will return "Hello" and "World" because it matched them individually.

Don't know how much better I can explain it than that, sorry. And hopefully some of the dev's will get the docs back up and included in the next beta or something. Personally, I can't wait to be able to use Holger's TrayMenu and Nutster's RegExp functions all in one script! :lmao:

Share this post


Link to post
Share on other sites

Well I want it to (thank you, btw :lmao:) check for only those 3 things... in any order.

It's for a game where items can have varying statistics and I only want to keep certain ones. For example, an item may have these statistics:

"200% defense 200% damage +4 Light radius +1 all skills +5 dex +14 mana"

But I'm only interested in Damage, All skills, and Mana.

So the * (SHOULD) work for me... but I suppose it didn't o:). Anyway, I don't care about the return, I just want to make sure the item has all of the specified traits.

Thanks a TON, I appriciate it greatly.

PS

I just noticed if I change the order of the traits I want, it messes it up... should I search for each trait individually?

$String = "200% defense 200% damage +4 Light radius +1 all skills +5 dex +14 mana "
$RegExp1 = StringRegExp($String, "([0-9]+% damage)")
$RegExp2 = StringRegExp($String, "([0-9]+ all skills")
$RegExp3 = StringRegExp($String, "([0-9]+ mana)")

If $RegExp1 = 1 AND $RegExp2 = 1 AND $RegExp3 = 1 Then
    MsgBox("","", "ALL FOUND!")
EndIf

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Well I want it to (thank you, btw :lmao:) check for only those 3 things... in any order.

It's for a game where items can have varying statistics and I only want to keep certain ones.  For example, an item may have these statistics:

But I'm only interested in Damage, All skills, and Mana.

So the * (SHOULD) work for me... but I suppose it didn't o:).  Anyway, I don't care about the return, I just want to make sure the item has all of the specified traits.

Thanks a TON, I appriciate it greatly.

PS

I just noticed if I change the order of the traits I want, it messes it up... should I search for each trait individually?

$String = "200% defense 200% damage +4 Light radius +1 all skills +5 dex +14 mana "
$RegExp1 = StringRegExp($String, "([0-9]+% damage)")
$RegExp2 = StringRegExp($String, "([0-9]+ all skills")
$RegExp3 = StringRegExp($String, "([0-9]+ mana)")

If $RegExp1 = 1 AND $RegExp2 = 1 AND $RegExp3 = 1 Then
    MsgBox("","", "ALL FOUND!")
EndIf

<{POST_SNAPBACK}>

Yes, you will have to use 3 seperate expressions, just like you've done there. So you just wanted a true/false for if the string had a number followed by "% damage" for example? You don't even need the brackets in your expressions then.

Share this post


Link to post
Share on other sites

Hey, I'm back!

Ok, \b is not implented in this version. You could implement it as \s?, perhaps?

$String = "200% defense 200% damage +4 Light radius +1 all skills +5 dex +14 mana"
$RegExp = StringRegExp($String, "([0-9]+% damage)[^+]*?\+([0-9]+ all skills)[^+]*?\+([0-9]+ mana)", 1)

For $i = 0 to UBound($RegExp) - 1
     MsgBox("","", $RegExp[$i] )
Next

Try that version and see if works any better.


David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

Thanks guys, but I only need this solution:

Dim $RegExp[3]

$String = "200% defense 200% damage +4 Light radius +1 all skills +5 dex  100% damage +14 mana "

$RegExp[0] = "[2-3][0-9]+% damage"
$RegExp[1] = "[0-9]+ all skills"
$RegExp[2] = "[0-9]+ mana"

$Check = _RegExp( $String, $RegExp )

If $Check = 1 Then
    MsgBox("","", "ALL FOUND!")
EndIf

Func _RegExp( $String, $Exp )
    Local $i, $Test
    
; If passed an array, loop through it and compare each string
    If IsArray($Exp) Then
        For $i = 0 to UBound($Exp) - 1
            $Test = StringRegExp( $String, $Exp[$i] )
            
            If $Test = 0 Then
                SetError (1)
                Return 0
            EndIf
        Next
        
        Return 1
    Else 
; Otherwise just do 1 string
        StringRegExp( $String, $Exp )
    EndIf
EndFunc


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Agh, more problems.

Figured I could move a little project I'm doing over to RegExp to make it easier, basically parsing commands:

SetTitle "AutoIT", "New Title", 1

Something like that.

I tried this code (tested here: http://www.quanetic.com/regex.php using posix style)

and I tested in AutoIT, it would not work... not returning an array. I even tried some code posted by Nutster.

$Input = '52, 105, "string",'
$Test = StringRegExp ( $Input, '([0-9]+|\".*\")\,', 3)

;MsgBox("","", $Test)

For $i = 0 to UBound($Test) - 1
    MsgBox("","", $Test[$i] )
Next

;$String = "200% defense 200% damage +4 Light radius +1 all skills +5 dex +14 mana"
;$RegExp = StringRegExp($String, "([0-9]+% damage)[^+]*?\+([0-9]+ all skills)[^+]*?\+([0-9]+ mana)", 1)

;For $i = 0 to UBound($RegExp) - 1
;    MsgBox("","", $RegExp[$i] )
;Next

BTW... I don't believe I've thanked you Nutster. Thank you, VERY much! This is insanely useful :lmao:


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

#133 ·  Posted (edited)

Why do you have the \ in there?

$Input = '52, 105, "string"'
$Test = StringRegExp ( $Input, '([0-9]+)|(".*")', 3)

For $i = 0 to UBound($Test) - 1
    MsgBox("","", $Test[$i] )
Next

Tadaa! :lmao:

Edited by Saunders

Share this post


Link to post
Share on other sites

#134 ·  Posted (edited)

Well thank you very much! o:)

EDIT:

How do I get it to avoid ending the strings with quotes preceded by '\'?

Example:

SetTitle "AutoIT", "Made by: \"Eric Carmichael\""

Pretty much how PHP works.

EDIT:

$Input = '52, 105, "string \" "'
$Test = StringRegExp ( $Input, '([0-9]+)|"(.*)"', 3)

For $i = 0 to UBound($Test) - 1
    MsgBox("","", $Test[$i] )
Next

Seems to work pretty well for that, I can just use StringReplace to fix it :lmao:

Edited by Insolence

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

More problems:

$Input = 'SetTitle "String", 1, 2,3, 5,  "Should return all of these"   ,"Literal use of quotes: \"\"" ' 

$arParameter = StringRegExp ( $Input, '(\w*) (([0-9]+)|(".*?[^\\]"))', 3)

For $i = 0 to UBound($arParameter) - 1
    MsgBox("","", $arParameter[$i] )
Next

I'm horrible at explaining things... running this should show the problems :lmao:

PS

I want to get rid of the start/end quotes... whenever I try to do that while keeping the quotes preceded by '\' it doesn't work correctly.


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

#136 ·  Posted (edited)

More problems:

$Input = 'SetTitle "String", 1, 2,3, 5,  "Should return all of these"   ,"Literal use of quotes: \"\"" ' 

; Description of the RegExp I am using:
;   Find the function name
;
;       Find a numeric parameter
;   OR 
;       A string parameter
;           Start at the first ", move to the next " that isn't preceded by a \
$arParameter = StringRegExp ( $Input,   '(\w*) '        & _     
                                        '(([0-9]+)'     & _     
                                        '|'             & _     
                                        '(".*?[^\\]"))'   _     
                                        , 3)

For $i = 0 to UBound($arParameter) - 1
    MsgBox("","", $arParameter[$i] )
Next

I'm horrible at explaining things... running this should show the problems :lmao:

PS

I want to get rid of the start/end quotes... whenever I try to do that while keeping the quotes preceded by \ it doesn't work correctly.

Edited by Insolence

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

BTW... I don't believe I've thanked you Nutster.  Thank you, VERY much!  This is insanely useful :lmao:

<{POST_SNAPBACK}>

You're welcome. I wrote it because I thought it would be useful for me. It was also a good challenge for me to write it.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

More problems:

$Input = 'SetTitle "String", 1, 2,3, 5,     "Should return all of these"    ,"Literal use of quotes: \"\"" ' 

; Description of the RegExp I am using:
;     Find the function name
;
;         Find a numeric parameter
;     OR 
;         A string parameter
;            Start at the first ", move to the next " that isn't preceded by a \
$arParameter = StringRegExp ( $Input,     '(\w*) '         & _     
                                        '(([0-9]+)'     & _     
                                        '|'             & _     
                                        '(".*?[^\\]"))'   _     
                                        , 3)

For $i = 0 to UBound($arParameter) - 1
    MsgBox("","", $arParameter[$i] )
Next

I'm horrible at explaining things... running this should show the problems :lmao:

PS

I want to get rid of the start/end quotes... whenever I try to do that while keeping the quotes preceded by \ it doesn't work correctly.

<{POST_SNAPBACK}>

I will try running this with the version I have at home. I made a bunch of bug fixes to the RegExp system that have not made it into the beta yet. We just have to be patient with Jon until he has the time to fix it.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

It works now and Saunders helped me a ton.

Don't worry about it :lmao:


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

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