KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to search all methods for regex pattern
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: March 6, 2023

Below is a method to search all method codes for a certain regex pattern. The method returns a sorted collection of unique occurrences that match the pattern.

/*
METHOD: searchMethodsForRegex
INPUT: regex pattern to search for in all methods
OUTPUT: collection of all unique occurrences of code that match the pattern
NOTE: returned collection will be sorted
*/
#DECLARE($pattern : Text)->$results : Collection
var $start; $pos; $length; $i : Integer
var $code; $found : Text
var $match : Boolean

ARRAY TEXT($methodPath; 0)
METHOD GET PATHS(Path all objects; $methodPath)

$results:=New collection
$start:=1

For ($i; 1; Size of array($methodPath))
   METHOD GET CODE($methodPath{$i}; $code)

   Repeat
     $match:=Match regex($pattern; $code; $start; $pos; $length)

     If ($match)
       $found:=Substring($code; $pos; $length)

       If ($results.indexOf($found)=-1)
         $results.push($found)
       End if

       $start:=$pos+$length

       // reset
       If ($start>Length($code))
         $match:=False
         $start:=1
       End if
     Else
       $start:=1
     End if

   Until ($match=False)
End for

$results.sort()

For example, to get all unique instances of untokenized plugin commands:

$pattern:="‘\\d\\d\\d\\d\\d;\\d{1,2}‘"
$res:=searchMethodsForRegex($pattern)

The result could look like this:

[“‘10128;1‘“, ”‘10128;12‘“, ”‘11512;32‘“, “‘11512;40‘“, “‘11999;1‘“]