ph_regexsnap3 PowerHome formula function
Description
Another function for using Regular Expressions to search for and "snap" (extract) data from a source. This version of the function uses C#.net regular expressions to perform the desired actions 
Syntax
ph_regexsnap3 ( as_pattern, as_snap, as_data, al_start, ai_occurence, ai_flags, ai_localstart, ai_locallength )
Argument Description
as_pattern String. The regular expression pattern to search and snap data
as_snap String. The snap pattern that dictates how the snapped data is returned. Use an empty string "" to just return all matches concatenated together
as_data String. The data you wish to perform the regular expression search and snap on
al_start Long. The starting position in the data at which the regular expression search will start. Use 1 to start searching at the beginning
ai_occurence Integer. The occurence of the particular data you wish to match. In a given as_data string, there may be multiple matches for a particular regular expression search. This parameter allows you to define which particular matching instance you would like to work with. Use a value of 1 to use the first instance. To return ALL instances, use a value of 0
ai_flags Integer. A flags parameter that controls how the regular expression is performed. See Usage below for details
ai_localstart Integer. The index of a local variable in which to store the start of data that is matched by the regular expression. Use 1 thru 10 for [LOCAL1] thru [LOCAL10]. If you don't want the start position stored in a local variable, use 0
ai_locallength Integer. The index of a local variable in which to store the length of data that is matched by the regular expression. Use 1 thru 10 for [LOCAL1] thru [LOCAL10]. If you don't want the length of matched data stored in a local variable, use 0
Return value
String. Returns the requested "snapped" data based upon the snap pattern in the as_snap parameter. 
Usage
Use this function to search a string for patterns of characters and return desired extracted portions of the matched search. Very useful for parsing HTML data returned by a ph_geturl1( ) function. This function can be used to just find the starting position and length of matched data for a particular occurrence or it can be used to extract portions of data that are searched for. A very good reference for the C# regular expressions that this function uses can be found here: https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference

To extract (or "snap") portions of a regex match, a "capturing" group is specifed in the as_pattern parameter. A capturing group is defined by enclosing the portion of the regular expression search you wish to capture in open "(" and close ")" parenthesis. To specify how the extraction is built into the return string, the as_snap pattern allows you to reference the capturing groups (numbered from left to right in the order that they appear in as_pattern starting with 1) by prefixing the capture group number with a dollar sign "$" character. You can also include additional text to separate or otherwise delimit the captured entries. A simple snap pattern to return the #1 capture group with a colon surrounded by space characters as a delimiter would be: "$1 : "

The ai_flags parameter controls how the regular expression search is done. The value for each desired option below would be added together to arrive at the proper ai_flags value. If none of the additional options are required, then use an ai_flags value of 0. For a detailed explanation of eachof the options, see this link: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regexoptions?view=netcore-3.1

  • 1 - Ignore Case
  • 2 - Multiline
  • 4 - Explicit Capture
  • 8 - Compiled
  • 16 - Single Line
  • 32 - Ignore Pattern Whitespace
  • 64 - Right to Left
  • 256 - ECMA Script
  • 512 - Culture Invariant
  • Examples
    The following examples demonstrate typical syntax/usage for this function.
    • ph_regexsnap3("([a-f]+)","","This is a bc test def",1,0,0,1,2) - Returns "abcedef". [LOCAL1] will contain the value 9 (the start position of the first match) and [LOCAL2] will contain the value 1 (the length of the first match) 
    • ph_regexsnap3("([a-f]+)","","This is a bc test def",1,2,0,1,2) - Returns "bc". [LOCAL1] will contain the value 11 (the start position of the requested second match) and [LOCAL2] will contain the value 2 (the length of the requested second match)
    • ph_regexsnap3("([a-f]+)","($1)","This is a bc test def",1,0,0,1,2) - Returns "(a)(bc)(e)(def)". [LOCAL1] will contain the value 9 (the start position of the first match) and [LOCAL2] will contain the value 1 (the length of the first match)