Howard Bullock
(KiX Supporter)
2003-09-10 04:12 PM
RFC: Regular Expression UDFs

For those of you interested in using Regular Expressions from within KiXtart, please review and comment on the code below. I wanted to get some feedback before formalizing them and posting to the UDF forum.


;
; Test to determine if a Regular Expression produces a match
;

?
? "**Test to determine if a Regular Expression produces a match**"
? 'RegExpTest("(\[Registry Value Create)(\d+)(\])", $text, 1)'
?
$text = "[Registry Value Create1][Registry Value Create22]"
if RegExpTest("(\[Registry Value Create)(\d+)(\])", $text, 1)
? "-Found match"
else
? "-No Match found"
endif

;
; Return array of arrays of matches
;
?
? "**Return array of arrays of matches**"
$Matches = RegExpFind("(\[Registry Value Create)(\d+)(\])", $text, 1, 1)

? "Number of matches = " + (1+ubound($Matches))
for each $match in $matches
? "Match = " + $match[0]
; Output submatches
for $x=1 to ubound($match)-1
? " SubMatch("+$x+") = " + $match[$x]
next
next



;
; Replace text using a RegExp
;
$OriginalText = "asdf123ds3frt56dtf-543l"
?
? "**Replace text using a RegExp**"
? "OriginalText = " + $OriginalText
?
? 'RegExpReplace("asdf123ds3frt56dtf-543l", "\d+", "?", 1, 1)'
? "Replaced text = " + RegExpReplace("asdf123ds3frt56dtf-543l", "\d+", "?", 1, 1)
?
? 'RegExpReplace("asdf123ds3frt56dtf-543l", "\d", "?", 1, 1)'
? "Replaced text = " + RegExpReplace("asdf123ds3frt56dtf-543l", "\d", "?", 1, 1)
?
? 'RegExpReplace("asdf123ds3frt56dtf-543l", "(\w+)-(\w+)", "$2-$1", 1, 1)'
? "Replaced text = " + RegExpReplace("asdf123ds3frt56dtf-543l", "(\w+)-(\w+)", "$2-$1", 1, 1)




?
?
$line = '416100 "Malcolm,JK" 534278 MALCOLJK'
$RegExPattern = '^(\d+)\s+"(\w+),(\w+)"\s+(\d+)\s+(\w+)$'
if RegExpTest($RegExPattern, $line, 1)
? "-Found match"
else
? "-No Match found"
endif
$Matches = RegExpFind($RegExPattern, $line, 1, 1)
?
? "Number of matches = " + (1+ubound($Matches))
for each $match in $matches
? "Match = " + $match[0]
; Output submatches
for $x=1 to ubound($match)-1
? " SubMatch("+$x+") = " + $match[$x]
next
next

Function RegExpTest($Pattern, $String, $IgnoreCase)

Dim $regEx, $Match, $Matches ; Create variable.

$regEx = createobject("VBscript.RegExp") ; Create a regular expression.

$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.

$RegExpTest = $regEx.Test($String) ; Execute test search.
EndFunction

Function RegExpFind($Pattern, $String, $IgnoreCase, $Global)

Dim $regEx, $Match, $Matches, $i, $j, $k ; Create variable.
Dim $Results[0]
$regEx = createobject("VBscript.RegExp") ; Create a regular expression.

$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.
$regEx.Global = val($Global) ; Set global applicability.

$Matches = $regEx.Execute($String) ; Execute search.

$k=0
For Each $Match in $Matches ; Iterate Matches collection.
Dim $MatchValue[0]
ReDim Preserve $Results[$k]

$MatchValue[0] = $Match.Value

if $Match.Submatches.count > 0
for $i=0 to $Match.Submatches.count
ReDim Preserve $MatchValue[$i+1]
$MatchValue[$i+1] = $Match.Submatches($i)
next
endif
$Results[$k] = $MatchValue
$k=$k+1
Next
$RegExpFind = $Results
EndFunction

Function RegExpReplace($String1, $Pattern, $String2, $IgnoreCase, $Global)

Dim $regEx ; Create variable.
Dim $Results[0]
$regEx = createobject("VBscript.RegExp") ; Create a regular expression.

$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.
$regEx.Global = val($Global) ; Set global applicability.

$RegExpReplace = $regEx.Replace($String1, $String2) ; Execute search.
EndFunction


Howard Bullock
(KiX Supporter)
2003-09-10 07:06 PM
Re: RFC: Regular Expression UDFs

I guess there really isn't any desire to use Regular Expression in KiXtart. At least there wasn't in July 2003.

http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=2;t=004454#000000

Maybe some desire has developed since then?

The code posted above is a complete demo. Just execute it in a dos window and examine the output then look at the code again.

[ 10. September 2003, 19:21: Message edited by: Howard Bullock ]


Kdyer
(KiX Supporter)
2003-09-10 11:25 PM
Re: RFC: Regular Expression UDFs

Howard,

I think that most of us (myself included) are not in this realm.. I know you are quite experienced with PERL..

I posted this link a week or so ago for a tutorial - http://www.regular-expressions.info

Maybe for me, I would be interested in it. I could think of a pretty interesting example where you would do a search and replace where you could increment the replacement.

Kent

[ 10. September 2003, 23:26: Message edited by: kdyer ]


Richard H.Administrator
(KiX Supporter)
2003-09-11 10:48 AM
Re: RFC: Regular Expression UDFs

This is very useful, but not a lot of people will realise how useful until you start creating elegant solutions using it.

The most obvious place it in input validation, but I'm sure that others will turn up soon.


Johan van Huyssteen_dup1
(Fresh Scripter)
2003-09-11 03:25 PM
Re: RFC: Regular Expression UDFs

From windows 9x nt 2000 and xp. You can use the follwoing command to import and export reg files its a hidden allot of us do not know about.

e.g.
import:

"command.com /c" type c:\hb.txt > c:\HBK.reg
"REGEDIT.EXE" /S c:\HBK.reg

export:
"command.com /c" type c:\hb.reg > c:\HB.txt
"regedit.exe" /e c:\HB.REG hkey_current_user\software\hummingbird

I pype it to txt to eaze the find replace of any txt in the reg file and convert it back to a reg file


Howard Bullock
(KiX Supporter)
2003-09-11 03:29 PM
Re: RFC: Regular Expression UDFs

What does your post have to do with Regular Expressions?

masken
(MM club member)
2005-10-23 01:49 PM
Re: RFC: Regular Expression UDFs

Howard, I know this is an old thread, but I use these UDF's quite extensively, think I maybe have found something in the RegExpFind() function, and it is that it only returns the first subset match (ie, the first set of groups in a regex query, where the expression matches several groups). Too long to explain here, catch me on MSN Howard

Howard Bullock
(KiX Supporter)
2005-10-23 07:51 PM
Re: RFC: Regular Expression UDFs

I would be interested in seeing your example code that demonstrates the issue. My test seems to work as expected.

Code:
$String = "ABC_xbc1bcA"
$Pattern = "bc"
$IgnoreCase = 1
$Global =1

$Matches = RegExpFind($Pattern, $String, $IgnoreCase, $Global)
?
? "Number of matches = " + (1+ubound($Matches))
for each $match in $matches
? "Match = " + $match[0]
; Output submatches
for $x=1 to ubound($match)-1
? " SubMatch("+$x+") = " + $match[$x]
next
next



masken
(MM club member)
2005-10-24 03:02 PM
Re: RFC: Regular Expression UDFs

hmm... sent you a PM

masken
(MM club member)
2005-10-25 08:17 AM
Re: RFC: Regular Expression UDFs

Sorry, my own fault with the danged Global switch

Anyhow, shouldn't this be finalized into one or several UDF's? Works like a charm I personally have them all collected in one "regex.udf".


Howard Bullock
(KiX Supporter)
2005-10-25 09:03 PM
Re: RFC: Regular Expression UDFs

You and Richard are the only two that have used or at least positively commented on them. Few seem interested.

masken
(MM club member)
2005-10-26 05:07 PM
Re: RFC: Regular Expression UDFs

That's simply cause they don't know how powerful regex is! Everyone should get RegexBuddy and see what it can do for matching text strings

NTDOCAdministrator
(KiX Master)
2005-10-26 06:26 PM
Re: RFC: Regular Expression UDFs

I have it, but don't use it too much in day to day use. But comes in handy when you do need it. I suppose a lot has to do with what type of work you're doing.

Howard, I would bet that it's also not used much because of the complexity of regular expressions in general. Once someone does use it though and gets good results from it, I'm sure they'll use it more and more.


masken
(MM club member)
2005-10-26 07:51 PM
Re: RFC: Regular Expression UDFs

yeah it's true that it has a somehwat high learning threshold. Doing basic stuff is quite easy though, if you just got some examples to look at. The documentation isn't very intuitive either, even though many have tried - at least I haven't found a good source.

btw, who uses KiX for loginscripts nowadays anyhow? It's me' general swiss army knife


Glenn BarnasAdministrator
(KiX Supporter)
2005-10-26 08:53 PM
Re: RFC: Regular Expression UDFs

Hmm?? I'm actually still using the "same basic" login script from 1995 / Kix 2.x. Well, the logic's the same - I finally rewrote it last month to replace the subroutines with UDFs, just as an exercise.

Of course, KiX is SO MUCH MORE than a login script processor! I'm not sure that "swiss army knife" even does it justice.

BTW - RegEx is "wicked cool" - it's one reason I still use VI / VIM for my script editor! (.! is the other!)

Glenn


Mart
(KiX Supporter)
2005-10-26 09:31 PM
Re: RFC: Regular Expression UDFs

Quote:


....
It's me' general swiss army knife
....





Can it open my beer? Swiss army knife has a bottle opener New UDF idea


Richard H.Administrator
(KiX Supporter)
2005-10-27 09:36 AM
Re: RFC: Regular Expression UDFs

Quote:

Quote:


....
It's me' general swiss army knife
....





Can it open my beer? Swiss army knife has a bottle opener New UDF idea




It can drink your beer too, and explain all the nuances of the flavour.