Sign in to follow this  
Followers 0
randallc

StringRegExpVBS.au3

10 posts in this topic

Hi,

I thought if confidence is down in inbuilt RegExp and RegExpReplace, we could just use vbs RegExp function wrapped;

returns the array of matches if possible;

What do you think?

Anyone who understands the functions better can please advise or modify, with thanks!

GHere's an example to show it is working.

;StringRegExpVBSExamples.au3
#include<StringRegExpVBS.au3>
$s_Replaced=_StringRegExpReplace('www.foo.co.uk.co.uk.co.uk','.co.uk','.com')
MsgBox(0,"","_StringRegExpReplace('www.foo.co.uk.co.uk.co.uk','.co.uk','.com')="&$s_Replaced)
$s_Replaced=_StringRegExpReplace('www.foo.co.uk.co.uk.co.uk','^[.\s]+co','.com')
$ar_Found=_StringRegExp('www.foo.co.uk.co.uk.co.uk','[\w\W\s]*?co');'^[.\s]+co')
;=========================================================================
$ar_Found=_StringRegExp('www.foo.co.uk.co.uk.co.uk','^[\w\W\s]*?co')
if not IsArray($ar_Found) then MsgBox(0,"","_StringRegExp('www.foo.co','.co.uk')="&$ar_Found)
if  IsArray($ar_Found) then _ArrayDisplay($ar_Found,"_StringRegExp('www.foo.co','.co.uk')")
;~ ;=========================================================================
$s_Replaced=_StringRegExpReplace('<td>12345</td>','<td>[\d]+</td>','.com')
MsgBox(0,"","_StringRegExpReplace('<td>12345</td>','<td>[\d]+</td>','.com')="&$s_Replaced)
$s_Replaced=_StringRegExpReplace('<td>12345</td>','<td>[\d]+</td>','.com')
MsgBox(0,"","_StringRegExpReplace('<td>12345</td>','<td>[\d]+</td>','.com')="&$s_Replaced)
$ar_Found=_StringRegExp('<td>12345</td>','<td>[\d]+</td>')
if not IsArray($ar_Found) then MsgBox(0,"","_StringRegExp('<td>12345</td>','<td>[\d]+</td>')="&$ar_Found)
if  IsArray($ar_Found) then _ArrayDisplay($ar_Found,"_StringRegExp('<td>12345</td>','<td>[\d]+</td>')")
$ar_Found=_StringRegExp("www.foo.co.uk.co.uk.co.uk",".co.uk")
if not IsArray($ar_Found) then MsgBox(0,"","_StringRegExp('www.foo.co.uk.co.uk.co.uk','.co.uk')="&$ar_Found)
if  IsArray($ar_Found) then _ArrayDisplay($ar_Found,'_StringRegExp("www.foo.co.uk.co.uk.co.uk",".co.uk")')
$ar_Found=_StringRegExp('www.foo.co','.co.uk')
if not IsArray($ar_Found) then MsgBox(0,"","_StringRegExp('www.foo.co','.co.uk')="&$ar_Found)
if  IsArray($ar_Found) then _ArrayDisplay($ar_Found,"_StringRegExp('www.foo.co','.co.uk')")
Best, Randall

Share this post


Link to post
Share on other sites



BTW, what's wrong with StringRegexp() ?

I have a bunch of scripts with it (and StringRegexpReplace), no problem found so far.

I'd like to understand :P

Regards,

Apzo.

Hi,

I thought if confidence is down in inbuilt RegExp and RegExpReplace, we could just use vbs RegExp function wrapped;

returns the array of matches if possible;

What do you think?

Anyone who understands the functions better can please advise or modify, with thanks!

GHere's an example to show it is working.

;StringRegExpVBSExamples.au3
#include<StringRegExpVBS.au3>
$s_Replaced=_StringRegExpReplace('www.foo.co.uk.co.uk.co.uk','.co.uk','.com')
MsgBox(0,"","_StringRegExpReplace('www.foo.co.uk.co.uk.co.uk','.co.uk','.com')="&$s_Replaced)
$s_Replaced=_StringRegExpReplace('www.foo.co.uk.co.uk.co.uk','^[.\s]+co','.com')
$ar_Found=_StringRegExp('www.foo.co.uk.co.uk.co.uk','[\w\W\s]*?co');'^[.\s]+co')
;=========================================================================
$ar_Found=_StringRegExp('www.foo.co.uk.co.uk.co.uk','^[\w\W\s]*?co')
if not IsArray($ar_Found) then MsgBox(0,"","_StringRegExp('www.foo.co','.co.uk')="&$ar_Found)
if  IsArray($ar_Found) then _ArrayDisplay($ar_Found,"_StringRegExp('www.foo.co','.co.uk')")
;~ ;=========================================================================
$s_Replaced=_StringRegExpReplace('<td>12345</td>','<td>[\d]+</td>','.com')
MsgBox(0,"","_StringRegExpReplace('<td>12345</td>','<td>[\d]+</td>','.com')="&$s_Replaced)
$s_Replaced=_StringRegExpReplace('<td>12345</td>','<td>[\d]+</td>','.com')
MsgBox(0,"","_StringRegExpReplace('<td>12345</td>','<td>[\d]+</td>','.com')="&$s_Replaced)
$ar_Found=_StringRegExp('<td>12345</td>','<td>[\d]+</td>')
if not IsArray($ar_Found) then MsgBox(0,"","_StringRegExp('<td>12345</td>','<td>[\d]+</td>')="&$ar_Found)
if  IsArray($ar_Found) then _ArrayDisplay($ar_Found,"_StringRegExp('<td>12345</td>','<td>[\d]+</td>')")
$ar_Found=_StringRegExp("www.foo.co.uk.co.uk.co.uk",".co.uk")
if not IsArray($ar_Found) then MsgBox(0,"","_StringRegExp('www.foo.co.uk.co.uk.co.uk','.co.uk')="&$ar_Found)
if  IsArray($ar_Found) then _ArrayDisplay($ar_Found,'_StringRegExp("www.foo.co.uk.co.uk.co.uk",".co.uk")')
$ar_Found=_StringRegExp('www.foo.co','.co.uk')
if not IsArray($ar_Found) then MsgBox(0,"","_StringRegExp('www.foo.co','.co.uk')="&$ar_Found)
if  IsArray($ar_Found) then _ArrayDisplay($ar_Found,"_StringRegExp('www.foo.co','.co.uk')")
Best, Randall

Share this post


Link to post
Share on other sites

Hey,

I'm not sure, myself, but there are frequent references on the forum to them being "buggy" and that being why they are not in the helpfile at present.... people changing scripts (eg enCodeIt) because of it etc...

Best, randall

Share this post


Link to post
Share on other sites

Interesting. I had looked at Wsh RegExp some time ago, but considered it builtin into the runtime such that could not be usable. Obviously I misjudged and you have shown it can be used.

Very nice randall.

Share this post


Link to post
Share on other sites

Incase anyone wishes to use the above UDFs, I created a (html page) table of patterns that can be used with the UDF's taken from the Script56.chm.

VBS_Regexp.html

:P

Share this post


Link to post
Share on other sites

Hey,

I'm not sure, myself, but there are frequent references on the forum to them being "buggy" and that being why they are not in the helpfile at present.... people changing scripts (eg enCodeIt) because of it etc...

Best, randall

$pattern= "(\*?)(.+)"

$string = "Gx"
$match = StringRegExp($string, $pattern, 3)
;should return "" and "G"
msgbox(0,$match[0],$match[1])

the example should return "" and "G". But due the buggy state no "" are returned when needed. This the main Bug I know of. :P

Share this post


Link to post
Share on other sites

As always randall, you've got another good idea. Please refresh my memory wrt using the vbscript trick.

As I remember, the vbs code is standard in all Windows releases since W2K and the argument for not using this type of UDL is that it is not universal within the MS Windows panaplay of OS's.

Note: This does not affect my users as all are at W2K or newer.

N.B. I think that most of my critisism of the AutoIt Regular Exp stuff is that it is not consistant with it's own documentation (thus, the failure within groups sited earlier) and there are holes wherein if it does work per the docs it may take many (10's of) seconds to accomplish it's task. I spend way too much time trying to find a pattern that will accomplish my needs without these weaknesses popping up. (Not crtitisizing Nustster, mind you. This is volunteer work remember).

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hi, tHanks!

I guess that is close to how my understanding is; although I don't profess to know this in great detail.

Firstly, I don't think the "scripting object" is available before Win 2000. This can easily be adjusted by changing the UDF over to run using Wscript rather than the scripting object and is easy enough to change if anyone wants to.

On the other hand, VBScript 5.0 is required and must be installed on any computer before Windows 2000 as well, I believe [also BartsPE]. This is not such a big deal though;

Does this answer your question?

Best, Randall.

Edited by randallc

Share this post


Link to post
Share on other sites

Hi Randall,

Yes, thanks for the clarification.

Regards, Jack

Share this post


Link to post
Share on other sites

Here is a Matches Collection test to play with to help with creating a matching regex pattern.

MsgBox(0, '', _RegExpMatchesCollectionTest("is.", "IS1 is2 IS3 is4"))

Func _RegExpMatchesCollectionTest($sPattern, $sString)
    Local $sCode, $oScriptControl
    $sCode =  'Function RegexMatches(sPattern, sString)' & _
        @LF & ' Dim oRegEx, oMatch, oMatches, RetStr' & _ ; Create variables.
        @LF & ' Set oRegEx = New RegExp' & _ ; Create regular expression.
        @LF & ' oRegEx.Pattern = sPattern' & _ ; Set pattern.
        @LF & ' oRegEx.IgnoreCase = True' & _ ; Set case sensitivity.
        @LF & ' oRegEx.Global = True' & _ ; Set Global option.
        @LF & ' Set oMatches = oRegEx.Execute(sString)' & _ ; Execute the search test.
        @LF & ' For Each oMatch in oMatches' & _
        @LF & '     RetStr = RetStr & "Match found at position "' & _
        @LF & '     RetStr = RetStr & oMatch.FirstIndex & vbCRLF' & _
        @LF & '     RetStr = RetStr & "Match Value is "' & _
        @LF & '     RetStr = RetStr & oMatch.Value & "." & vbCRLF & vbCRLF' & _
        @LF & ' Next' & _
        @LF & ' RegexMatches = RetStr' & _
        @LF & 'End Function'
    $oScriptControl = ObjCreate("ScriptControl")
    If Not @error Then
        $oScriptControl.Language = "VBScript"
        $oScriptControl.AddCode ($sCode)
        Return $oScriptControl.Run ('RegexMatches', $sPattern, $sString)
    Else
        SetError(1, 0, $sString)
    EndIf
EndFunc

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