Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Regular Expression Testing


  • Please log in to reply
138 replies to this topic

#121 djek

djek

    Seeker

  • Active Members
  • 35 posts

Posted 08 February 2005 - 08:48 PM

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:







#122 Jon

Jon

    Up all night to get lucky

  • Administrators
  • 10,470 posts

Posted 08 February 2005 - 08:52 PM

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:

<{POST_SNAPBACK}>

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

#123 djek

djek

    Seeker

  • Active Members
  • 35 posts

Posted 08 February 2005 - 09:55 PM

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

#124 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 21 February 2005 - 01:09 AM

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.

#125 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

Posted 22 February 2005 - 07:37 PM

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.

#126 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 22 February 2005 - 10:05 PM

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, 23 February 2005 - 03:01 AM.

"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.

#127 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

Posted 23 February 2005 - 08:05 AM

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:

#128 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 23 February 2005 - 03:41 PM

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.

#129 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

Posted 25 February 2005 - 06:00 PM

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.

#130 Nutster

Nutster

    Developer at Large

  • Developers
  • 1,450 posts

Posted 25 February 2005 - 10:17 PM

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...


#131 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 26 February 2005 - 12:04 AM

Thanks guys, but I only need this solution:
Plain Text         
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.

#132 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 05 March 2005 - 06:06 AM

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.

#133 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

Posted 05 March 2005 - 07:05 PM

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, 05 March 2005 - 07:08 PM.


#134 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 05 March 2005 - 09:10 PM

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, 05 March 2005 - 10:21 PM.

"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.

#135 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 05 March 2005 - 10:39 PM

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.

#136 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 05 March 2005 - 10:39 PM

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, 06 March 2005 - 01:44 AM.

"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.

#137 Nutster

Nutster

    Developer at Large

  • Developers
  • 1,450 posts

Posted 08 March 2005 - 10:42 PM

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...


#138 Nutster

Nutster

    Developer at Large

  • Developers
  • 1,450 posts

Posted 08 March 2005 - 10:52 PM

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...


#139 Insolence

Insolence

    Not distastefully arrogant

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 09 March 2005 - 12:40 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users