#76617  - 2003-09-10  04:12 PM  
 
RFC: Regular Expression UDFs 
 
 
 
 
Howard Bullock  
KiX Supporter
 
 
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  
 
  
Top 
 
 
 
 
 
 
 
Moderator:  Glenn Barnas , NTDOC , Arend_ , Jochen , Radimus , Allen , ShaneEP , Ruud van Velsen , Mart   
 
 
 
0 registered
and 360 anonymous users online.