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.
- Function notation
- Method notation
matches(value: string, pattern: regexp): bool
(value: string).matches(pattern: regexp): bool
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
value | string | true | The string to test |
pattern | regexp | true | The 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
- Function notation
- Method notation
filter !matches(msg_structured, /User [a-z|A-Z]+ bought d+ [a-z|A-Z]+/)
filter !msg_structured.matches(/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
- Function notation
- Method notation
filter matches(level, re2'(?i)error|warn(ing)?')
filter level.matches(re2'(?i)error|warn(ing)?')
Example output
{ "level": "ERROR" }
{ "level": "Warning" }