Page 1 of 1 1
Topic Options
#76617 - 2003-09-10 04:12 PM RFC: Regular Expression UDFs
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#76618 - 2003-09-10 07:06 PM Re: RFC: Regular Expression UDFs
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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 ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#76619 - 2003-09-10 11:25 PM Re: RFC: Regular Expression UDFs
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
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 ]
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#76620 - 2003-09-11 10:48 AM Re: RFC: Regular Expression UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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.

Top
#76621 - 2003-09-11 03:25 PM Re: RFC: Regular Expression UDFs
Johan van Huyssteen_dup1 Offline
Fresh Scripter

Registered: 2003-08-28
Posts: 22
Loc: RSA
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

Top
#76622 - 2003-09-11 03:29 PM Re: RFC: Regular Expression UDFs
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
What does your post have to do with Regular Expressions?
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#76623 - 2005-10-23 01:49 PM Re: RFC: Regular Expression UDFs
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
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

Edited by masken (2005-10-23 01:51 PM)
_________________________
The tart is out there

Top
#76624 - 2005-10-23 07:51 PM Re: RFC: Regular Expression UDFs
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#76625 - 2005-10-24 03:02 PM Re: RFC: Regular Expression UDFs
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
hmm... sent you a PM
_________________________
The tart is out there

Top
#76626 - 2005-10-25 08:17 AM Re: RFC: Regular Expression UDFs
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
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".
_________________________
The tart is out there

Top
#76627 - 2005-10-25 09:03 PM Re: RFC: Regular Expression UDFs
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You and Richard are the only two that have used or at least positively commented on them. Few seem interested.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#76628 - 2005-10-26 05:07 PM Re: RFC: Regular Expression UDFs
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
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
_________________________
The tart is out there

Top
#76629 - 2005-10-26 06:26 PM Re: RFC: Regular Expression UDFs
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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.

Top
#76630 - 2005-10-26 07:51 PM Re: RFC: Regular Expression UDFs
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
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


Edited by masken (2005-10-26 07:54 PM)
_________________________
The tart is out there

Top
#76631 - 2005-10-26 08:53 PM Re: RFC: Regular Expression UDFs
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#76632 - 2005-10-26 09:31 PM Re: RFC: Regular Expression UDFs
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Quote:


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





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

- Chuck Norris once sold ebay to ebay on ebay.

Top
#76633 - 2005-10-27 09:36 AM Re: RFC: Regular Expression UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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.

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.067 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org