Function: break_text

Function: break_text


 break_text(some_string, match: regular expression, in_context: regular expression)

Break text by only keeping the parts of the input that match the regular expression. The function returns a list of parts.

If in_context is given, the context must also match the string where the match is represented as <match>.

When the break_text is used many times with the same match or in_context values it is more efficient to declare these as default arguments:

 my_break := break_text@(match: "something")
 my_break("input") # called many times

This way the regular expression is only compiled once.

Filter vs. break

The function filter_text is very similar to break_text, instead of returning a list it concatenates the items. So for example where break_text returns ["a","b","c"], filter_text would return "abc". In fact, filter_text could be implemented as

 filter_text := { for part in break_text() do part }

Split vs. break

The function split_text does the opposite of break_text, keeping the parts between the regular expression matches, instead of the matching text itself.


Parameter Type Description
input string String to replace in.
match regex Regular expression to match.
in_context regex (optional) Context to match


 break_text(match: "a",     "banana")  ==  ["a","a","a"]
 break_text(match: "na|.",  "banana")  ==  ["b","a","na","na"]
 break_text(match: "ap",    "banana")  ==  []
 break_text(match: "/",     "a/b/c")   ==  ["/","/"]
 break_text(match: "[^/]+", "a/b/c")   ==  ["a","b","c"]

 f := break_text@(match: "xx+")
 f("xyzxxxxyyzz") == ["xxxx"]

See also

filter_text Keep only the text matching a regular expression.
split_text Split text into parts separated by a regular expression.