PreviousUpNext

5.5.44  Regular expressions

See if a string matches a regular expression:

    eval:  "foo bar zot" =~ ./^f.*a.*t$/;
    TRUE
    eval:  "foo bar zot" =~ ./^f.*q.*t$/;
    FALSE

Find location of first ’foo’ in a string:

    eval:  strlen (regex::find_first_match_to_ith_group 1 ./^(.*)foo/ "the fool on the hill");
    THE 4

Select all strings in a list matching a regular expression:

    eval:  filter {. #file =~ ./ee/; } [ "red", "green", "blue" ];
    ["green"]

Drop leading and trailing whitespace from a string:

    eval:  regex::find_first_match_to_ith_group 1 ./^\s*(\S.*\S)\s*$/ " foo ";
    THE "foo"

    eval:  trim  " foo ";                             # Better!
    "foo"

    eval:  makelib::scripting_globals::trim  " foo ";   # In programs.
    "foo"

Match ’regex’ once against ’text’ and return THE matched substring, returning NULL if no match was found:

    eval:  regex::find_first_match_to_regex ./f.t/ "the fat father futzed"
    THE "fat" 

Return all substrings of ’text’ which match ’regex’.

    eval:  regex::find_all_matches_to_regex ./f.t/ "the fat father futzed"; 
    ["fat", "fat", "fut"]

Match ’regex’ once against ’text’. Return NULL if no match found, else THE list of substrings constituting the match.

    eval:  regex::find_first_match_to_regex_and_return_all_groups ./(f.)(t)/ "the fat father futzed";
    THE ["fa", "t"]

For each match of the regex, return what the second set of parens matched:

    eval:  regex::find_all_matches_to_regex_and_return_values_of_ith_group 2 ./(f)(.)t/ "the fat father futzed";
    ["a", "a", "u"]

Replace first match of regex by given substring:

    eval:  regex::replace_first ./f.t/ "FAT" "the fat father futzed";
    "the FAT father futzed"

Replace all matches of regex by given substring:

    eval:  regex::replace_all ./f.t/ "FAT" "the fat father futzed";
    "the FAT FATher FATzed"

Find first match of regex in text. Pass match as a list of strings to given function. Splice returned string into text in place of match:

    eval:  regex::replace_first_via_fn ./(f.t)/ {. toupper (head #matchlist); }  "the fat father futzed";
    "the FAT father futzed"

As above, but process all matches of regex in text:

    eval:  regex::replace_all_via_fn ./(f.t)/ {. toupper (head #matchlist); }  "the fat father futzed";
    "the FAT FATher FUTzed"

See also:

Perl5 Regular Expressions Library Reference.
bare-essentials tutorial.
full-monte tutorial.
src/lib/regex/glue/regular-expression-matcher.api

Comments and suggestions to: bugs@mythryl.org

PreviousUpNext