randallc Posted September 4, 2006 Posted September 4, 2006 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 ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW
Apzo Posted September 4, 2006 Posted September 4, 2006 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 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 All the pop3 functions.Rsync your files on your USB key (or anywhere else)
randallc Posted September 4, 2006 Author Posted September 4, 2006 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 ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW
MHz Posted September 4, 2006 Posted September 4, 2006 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.
MHz Posted September 5, 2006 Posted September 5, 2006 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
jpm Posted September 5, 2006 Posted September 5, 2006 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.
JackB Posted September 5, 2006 Posted September 5, 2006 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).
randallc Posted September 6, 2006 Author Posted September 6, 2006 (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 September 6, 2006 by randallc ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW
JackB Posted September 6, 2006 Posted September 6, 2006 Hi Randall, Yes, thanks for the clarification. Regards, Jack
MHz Posted September 6, 2006 Posted September 6, 2006 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now