Regular expressions

Regular expressions are a kind of mini-language used to perform pattern matching within strings. There are the following built-in regular functions in Silk:
_fun("regex_match", str, pattern, option)
regex_match attempts to match a regular expression to an entire character sequence, it returns an array which contains match results if a match exists, null otherwise.
str is the target string, pattern is the regular expression, option used to determine how regular expressions behave, it can be ignored.
str="Hello_2021";
pattern="(.{5})_(\\d{4})"//Match 5 characters + _ + 4 numbers
r=_fun("regex_match",str,pattern);
if(r)
{
    print("match result:");
    for(i=0;i<r.size();i++)
        print(r[i]);
}
Result:
match result:
Hello_2021
Hello
2021

The parameter option has the following options:
  • EMCAScript: Default option, use the ECMAScript regular expression grammar.
  • icase: Character matching should be performed without regard to case. 
  • nosubs: When performing matches, all marked sub-expressions (expr) are treated as non-marking sub-expressions.
  • collate: Character ranges of the form "[a-b]" will be locale sensitive. 
We can combinate multiple options with seperator '|':
str="Hello_2021_y";
pattern="(.{5})_(\\d{4})_Y";
    r=_fun("regex_match",str,pattern,"icase|nosubs"); //ignore case, and no sub expression in the result
if(r)
{
    for(i=0;i<r.size();i++)
        print(r[i]);
}


_fun("regex_search", str, pattern, option)
regex_search attempts to find a regular match by matching a regular expression to any part of a character sequence, it returns an array which contains match results if a match exists, null otherwise.
str is the target string, pattern is the regular expression, option used to determine how regular expressions behave, it can be ignored.
str="123";
pattern="\\d";
r=_fun("regex_search",str,pattern,"icase");
if(r)
{
    print("search result:");
    //print the match results
    for(i=0;i<r.size();i++)
        print(r[i]);
}
Result:
search result:
1

_fun("regex_searchall", str, pattern, option)
regex_searhcall attempts to find all regular matches by matching a regular expression to any part of a character sequence, it returns an array which contains match results if a match exists, null otherwise.
str is the target string, pattern is the regular expression, option used to determine how regular expressions behave, it can be ignored.
str="Goodbye 2020, hello 2021";
pattern="\\d{4}";//Match 4 numbers
r=_fun("regex_searchall",str,pattern);
if(r)
{
    print("searchall result:");
    //print the result
    for(i=0;i<r.size();i++)
        print(r[i]);
}
Result:
searchall result:
2020
2021


_fun("regex_replace", str, pattern, replace, option)
regex_replace attempts to replace all regular matches in a character sequence, it returns a new string with the replacements applied.
str is the target string, pattern is the regular expression, replace is the replacement text, option used to determine how regular expressions behave, it can be ignored.
str="Hello_2021";
pattern="(.{3})(.{2})_(\\d{4})";//Match 5 characters + _ + 4 numbers
r=_fun("regex_replace",str,pattern,"$1$3");//Replace the string with the first and third regular expressions
print("replace result:",r);
Result:
replace result: Hel2021