Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Function

grep — search strings inside of values

Synopsis

grep(re: string e: any) -> bool

Description

The grep function searches all of the strings in its input value e using the re argument, which is a regular expression. If the pattern matches for any string, then the result is true. Otherwise, it is false.

Note

String matches are case insensitive while regular expression and glob matches are case sensitive. In a forthcoming release, case sensitivity will be expressible for all three pattern types.

The entire input value is traversed:

  • for records, each field name is traversed and each field value is traversed or descended if a complex type,
  • for arrays and sets, each element is traversed or descended if a complex type, and
  • for maps, each key and value is traversed or descended if a complex type.

Examples


Reach into nested records

# spq
grep("baz", this)
# input
{foo:10}
{bar:{s:"baz"}}
# expected output
{bar:{s:"baz"}}

It only matches string fields

# spq
grep("10", this)
# input
{foo:10}
{bar:{s:"baz"}}
# expected output

Match a field name

# spq
grep("foo", this)
# input
{foo:10}
{bar:{s:"baz"}}
# expected output
{foo:10}

Regular expression

# spq
grep("foo|baz", this)
# input
{foo:10}
{bar:{s:"baz"}}
# expected output
{foo:10}
{bar:{s:"baz"}}

Regular expression with a non-this argument

# spq
grep('b.*', s)
# input
{s:"bar"}
{s:"foo"}
{s:"baz"}
{t:"baz"}
# expected output
{s:"bar"}
{s:"baz"}