If you like regex but find the tediousness of checking array lengths, 
substring, etc. to be a turnoff, here’s a convenience method to find within a 
string using regex, returning the first grouping match if there is one, or the 
entire match.

Use as:

$text:=STR_Find_w_Regex 
("<https://this.is.stuff/somewhere?a=1&b=2>asdf";"<(.*)>”)
// gets the url, within the brackets

$sample:="https://this.is.stuff/some/object/id/DqjL4zOo9oVoo7MNoM8rHnkeV";
$text:=STR_Find_w_Regex($sample;"\\/([^\\/]+)$")
// get the last part of the string, after the last “/“



  // Method: STR_Find_w_Regex
  // 
  // Finds by regex, returning first grouping or entire capture
  //
  // ----------------------------------------------------
C_TEXT($0;$found)
C_TEXT($1;$text)
$text:=$1
C_TEXT($2;$regex)
$regex:=$2
  // ----------------------------------------------------
ARRAY LONGINT($positionsMatched;0)
ARRAY LONGINT($lengthsMatched;0)
  // ----------------------------------------------------
If (Match regex($regex;$text;1;$positionsMatched;$lengthsMatched))
        If (Size of array($positionsMatched)>0)
                $found:=Substring($text;$positionsMatched{1};$lengthsMatched{1})
        Else 
                $found:=Substring($text;$positionsMatched{0};$lengthsMatched{0})
        End if 
End if 

$0:=$found


—
Jim Crate

**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to