Jump to content

Regular Expression Testing


Nutster
 Share

Recommended Posts

David, how does one go about using the {x,y} feature?  I've tried this but it doesn't work:

$string = 12
$res = StringRegExp($string, "([0-9]{2})")
If Not @error Then MsgBox(4096, "", $res[0])

It doesn't work, however.

<{POST_SNAPBACK}>

I will review that tonight. {2} should work. Hmm, :lmao: I know I tested {2,5}, {2,}. Did I test {,4} and {3}? Checks test file that was just uploaded to http://www.autoitscript.com/fileman/users/Nutster/ Nope. I will fix and upload to Jon.

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

Link to comment
Share on other sites

  • Replies 138
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Might be a bug because the following works:

$string = 12

$res = StringRegExp($string, "([0-9]{2,2})")

If Not @error Then MsgBox(4096, "", $res[0])

<{POST_SNAPBACK}>

With the code I submitted to Jon on Sunday, the following changes should be made:

$string = 12
$res = StringRegExp($string, "([0-9][b]{2,2}[/b])", 1)
If @Error = 0 and @Extended Then MsgBox(4096, "", $res[0])

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

Link to comment
Share on other sites

This is fixed in the submission I made to Jon a few minutes ago.

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

Link to comment
Share on other sites

Please,Can you send here a bunch of easy examples of regexp, to see when it matches and when it doesnt match.

Edited by BasicOs
Autoit.es - Foro Autoit en Español Word visitors Image Clustrmap image: - Football Spanish team - Spanish team: Casillas, Iniesta, Villa, Xavi, Puyol, Campdevilla, etc..Programando en Autoit+Html - Coding Autoit-Html - Arranca programas desde Internet - Preprocesador de Autoit a http
Link to comment
Share on other sites

Check out http://www.autoitscript.com/fileman/users/Nutster which is where I put my test script, called Test RegExp 3.au3 . Have fun with it. Just put the patterns you want to test in the calls to RegExpAsk() UDF.

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

Link to comment
Share on other sites

Check out http://www.autoitscript.com/fileman/users/Nutster which is where I put my test script, called Test RegExp 3.au3 .  Have fun with it.  Just put the patterns you want to test in the calls to RegExpAsk() UDF.

<{POST_SNAPBACK}>

I guess somebody will wish me to hell because I added COM support.

Why? See the following (working) code:

; Regular Exp[b][/b]ression test using VBScript.RegExp object
;
; Requirements:
; AutoIt 1.0.103 with COM extensions
; VBscript.DLL version 5.0 or higher.
;
; Source: http://msdn.microsoft.com/library/en-us/script56/html/vsobjregexp.asp


Func RegExpTest($patrn, $strng)

   $regEx = CreateObject("VBScript.RegExp"); Create a regular exp[b][/b]ression.

   $regEx.Pattern = $patrn  ; Set pattern.
   $regEx.IgnoreCase = 1    ; Set case insensitivity: True.
   $regEx.Global = 1        ; Set global applicability: True.
   $Matches = $regEx.Execute($strng)  ; Execute search.

   For $Match in $Matches  ; Iterate Matches collection.
      $RetStr = $RetStr & "Match found at position "
      $RetStr = $RetStr & $Match.FirstIndex & ". Match Value is '"
      $RetStr = $RetStr & $Match.Value & "'." & @CRLF
   Next

   Return $RetStr
EndFunc

; Example usage:

MsgBox(0,"Test RegExp", RegExpTest("is.", "IS1 is2 IS3 is4"))

OK, I admit: it requires a VBscript object installed on the system.

And that's just what we DON'T want to use in AutoIt ;-)

You may shoot me now.

Regards,

-Sven

Link to comment
Share on other sites

SlimShady: It is on the TO DO list. Right now I am working on StringRegExpReplace. I still have to figure out how to implement the pipe.

Hmm, I wonder... Just worked how to do it, maybe. :lmao:

On totally unrelated note, I saw a bull called Slim Shady recently with a 1-38 riding record.

Edited by Nutster

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

Link to comment
Share on other sites

  • Administrators

SlimShady:  It is on the TO DO list.  Right now I am working on StringRegExpReplace.  I still have to figure out how to implement the pipe. 

Hmm, I wonder... Just worked how to do it, maybe.  :lmao:

On totally unrelated note, I saw a bull called Slim Shady recently with a 1-38 riding record.

Is that literally bull riding? Like you see on the ads? 38 people mashed up vs. 1 who managed it? Nice. o:)
Link to comment
Share on other sites

Is that literally bull riding?  Like you see on the ads?  38 people mashed up vs. 1 who managed it?  Nice. o:)

<{POST_SNAPBACK}>

Yup. Well 38 who could not stay on for 8 seconds and one incredibly lucky guy who managed to pull it off (and not pull his arm from his shoulder). :lmao:

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

Link to comment
Share on other sites

David, may I suggest that instead of using ?? to match the shortest match, - be used instead?  Lua uses that as its equivalent "operator" and it kind of makes sense as a compliment to +.

<{POST_SNAPBACK}>

? is used in both contexts in some regular expression systems. The ? after a regular character means 0 or 1 time and the ? after the repeatiing specifier means repeat the smallest number of times that still works.

I have not looked at LUA's pattern matching yet. I probably should.

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

Link to comment
Share on other sites

Food for thought.

Regular Expressions in SciTE. (uses Lua extensions)

Regular Exp[b][/b]ressions in SciTE 
Purpose
Regular exp[b][/b]ressions can be used for searching for patterns rather than literals. For example, it is possible to search for variables in SciTE property files, which look like $(name) with the regular exp[b][/b]ression:
\$([a-z.]+) 

Replacement with regular exp[b][/b]ressions allows complex transformations with the use of tagged exp[b][/b]ressions. For example, pairs of numbers separated by a ',' could be reordered by replacing the regular exp[b][/b]ression:
\([0-9]+\),\([0-9]+\)
with:
\2,\1 

Syntax

01. char matches itself, unless it is a special character (metachar): . \ [ ] * + ^ $ 
02. . matches any character. 
03. \ matches the character following it, except when followed by a left or right round bracket, a digit 1 to 9 or a left or right angle bracket. (see [7], [8] and [9]) It is used as an escape character for all other meta-characters, and itself. When used in a set ([4]), it is treated as an ordinary character. 
04. [set] matches one of the characters in the set. If the first character in the set is "^", it matches a character NOT in the set, i.e. complements the set. A shorthand S-E is used to specify a set of characters S up to E, inclusive. The special characters "]" and "-" have no special meaning if they appear as the first chars in the set. examples: match: [a-z] any lowercase alpha [^]-] any char except ] and - [^A-Z] any char except uppercase alpha [a-zA-Z] any alpha 
05. * any regular exp[b][/b]ression form [1] to [4], followed by closure char (*) matches zero or more matches of that form. 
06. + same as [5], except it matches one or more. 
07. a regular exp[b][/b]ression in the form [1] to [10], enclosed as \(form\) matches what form matches. The enclosure creates a set of tags, used for [8] and for pattern substitution. The tagged forms are numbered starting from 1. 
08. a \ followed by a digit 1 to 9 matches whatever a previously tagged regular exp[b][/b]ression ([7]) matched. 
09. \< a regular exp[b][/b]ression starting with a \< construct \> and/or ending with a \> construct, restricts the pattern matching to the beginning of a word, and/or the end of a word. A word is defined to be a character string beginning and/or ending with the characters A-Z a-z 0-9 and _. It must also be preceded and/or followed by any character outside those mentioned. 
10. a composite regular exp[b][/b]ression xy where x and y are in the form [1] to [10] matches the longest match of x followed by a match for y. 
11. ^ a regular exp[b][/b]ression starting with a ^ character $ and/or ending with a $ character, restricts the pattern matching to the beginning of the line, or the end of line. [anchors] Elsewhere in the pattern, ^ and $ are treated as ordinary characters. 

Acknowledgments
Most of this documentation was originally written by Ozan S. Yigit.
Additions by Neil Hodgson.
All of this document is in the public domain.
Link to comment
Share on other sites

Hey Nutster - looking good!

Any chance of standardising on the array-structure that is returned? For example, StringSplit has the really cool feature of placing the index to the last element in instance [0] .. much neater than looping with Ubound($aArray) - 1

Please?

:lmao:

Link to comment
Share on other sites

  • 2 weeks later...

Hey Nutster - looking good!

Any chance of standardising on the array-structure that is returned? For example, StringSplit has the really cool feature of placing the index to the last element in instance [0] .. much neater than looping with Ubound($aArray) - 1

Please?

:lmao:

<{POST_SNAPBACK}>

Thank you, but I still do not like that "feature" of StringSplit. Try this:

$selections = StringRegExp("This is another test.  Ask a question; get an answer.", "(\<[AEIOUaeuio]\w*[^AEIOUaeiou]\>)", 3)
For $i = 0 to UBound($selections)-1 ; only gets evaluated once

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

Link to comment
Share on other sites

Hey Nutster, is there any chance of incorporating groups into StringRegExpReplace? I'd really like to be able to do something like:

StringRegExpReplace('Go to http://google.com okay?', 'http://([^ ]+?)', '<a href="http://\\1">\\1</a>')

Which would return:

Go to <a href="http://google.com">google.com</a> okay?

See where I'm going? I was actually expecting this from the function when I heard you were working on it and thus was surprised when it didn't have this ability.

Also, check out my bug report here if you get some time.

Link to comment
Share on other sites

Hey Nutster, is there any chance of incorporating groups into StringRegExpReplace? I'd really like to be able to do something like:

StringRegExpReplace('Go to http://google.com okay?', 'http://([^ ]+?)', '<a href="http://\\1">\\1</a>')

Which would return:

Go to <a href="http://google.com">google.com</a> okay?

See where I'm going? I was actually expecting this from the function when I heard you were working on it and thus was surprised when it didn't have this ability.

Also, check out my bug report here if you get some time.

<{POST_SNAPBACK}>

I am going to work on back-referencing next. It will take a not so little bit of work. That will do what you are asking for.

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

Link to comment
Share on other sites

Request:

Make the OR operator work using the pipe character |.

<{POST_SNAPBACK}>

Written, tested and submitted to Jon.

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...