Qu

grep

Builtin index · Collections & strings

result = grep(s, pattern, [invert=])

Input: s, pattern, invert · Output: result

What it is

s is a Str (often multi-line) or List of lines; pattern is a regex Str; optional named invert (bool, default false) selects non-matching lines instead.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = grep(s, pattern, [invert=])
s Str or Lists is a Str (often multi-line) or List of lines.
pattern Strpattern is a regex Str.
invert bool, optionaloptional named invert (bool, default false) selects non-matching lines instead.
result List
Returns a List of the matching (or non-matching) LINES — regex_find_all returns the matched substrings themselves, a different thing.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

print(tail("abcdefgh", 3))    # "fgh"

log = "INFO start\nERROR disk full\nINFO retry\nERROR timeout"
print(grep(log, "ERROR"))     # ("ERROR disk full", "ERROR timeout")

output
fgh
("ERROR disk full", "ERROR timeout")

from collections-strings

How it works

grep(s, pattern) — the LINES that match, which is what the tool of that name does and therefore what the word means. regex_find_all is the one that returns the matches themselves.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.