Skip to main content

matches

Description

Returns true if a regular expression matches anywhere in a string. The match does not have to cover the whole string. In an re2'...' pattern, ^ and $ match the start and end of the whole value, so anchoring requires a match of the whole value. In a Ruby dialect /.../ pattern, they match at every line.

Syntax

Like many functions in DataPrime, matches supports two notations, function and method. These interchangeable forms allow flexibility in how you structure expressions.

matches(value: string, pattern: regexp): bool

Arguments

NameTypeRequiredDescription
valuestringtrueThe string to test
patternregexptrueThe regular expression to test against

Example 1

Detect malformed fields

Consider the following documents:

{ "msg_structured": "User Chris bought 10 Sunglasses" }
{ "msg_structured": "User James bought 1 Bed" }
{ "msg_structured": "User Rick won't give you up" }

The third entry is incomplete. Use matches to filter out malformed values:

Example query

filter !matches(msg_structured, /User [a-z|A-Z]+ bought d+ [a-z|A-Z]+/)

Example output

{ "msg_structured": "User Rick won't give you up" }

Example 2

Match a level name in any case with an RE2 pattern

Consider the following documents:

{ "level": "ERROR" }
{ "level": "Warning" }
{ "level": "info" }

Write the pattern as an RE2 literal and set the case-insensitive flag inline to keep the error and warning levels:

Example query

filter matches(level, re2'(?i)error|warn(ing)?')

Example output

{ "level": "ERROR" }
{ "level": "Warning" }
Last updated on
Was this page helpful?