Regular expressions in Neptune
In Neptune, you can use regular expressions (regex) to do the following:
- Filter widgets by attribute name in Charts, Side-by-side, and Reports.
- Configure a chart widget to dynamically display matching metrics in a dashboard or report.
- Match names or string attributes when building queries in the runs table or via API.
Regex engine details
Neptune uses the RE2 library to handle regular expressions.
For performance and security reasons, RE2 implements a subset of features commonly offered by regex engines. For example, backreferences and look-around assertions are not supported. For details, see the RE2 documentation.
Usage
To use regular expressions in the Neptune web app:
- In the search bar, click the icon so that it's active, or
- Use the / + key shortcut
Compound expressions
To match text against multiple expressions at once, join them with the compound operators AND
, AND NOT
and OR
.
You can also alternate regular expressions with a pipe. For example, _seagull$|_kittiwake$
is equivalent to _seagull$
OR
_kittiwake$
, preferring the first match from the left.
Edit an expression
To edit a regular expression or operator:
- Double-click on the part to edit, or
- Navigate to it with the arrow keys and press
Query builder
In the query builder for the experiments table, to match a String
value against a regex:
-
Ensure that the search bar is in Query mode.
-
Select a string attribute from the list.
For example,
sys/description
is an auto-generated string attribute. For how to create custom string attributes, see Log metadata: Text. -
Select the matches or not matches operator.
-
Enter a regular expression.
For details, see Experiments table: Searching and filtering runs.
NQL query in fetcher API
To match a logged string against a regular expression, use the MATCHES
or NOT MATCHES
operator in the query
argument:
project.fetch_runs_df(
query=r'`parameters/optimizer`:string MATCHES "Ada\\w+"'
)
For details, see NQL reference: String.
Syntax reference
To construct a regular expression, use:
-
literal characters, such as
seagull
-
metacharacters, such as
*
,+
, or|
Escape metacharacters with a backslash. For example, to match a literal plus sign, use
\+
.
The following tables list some of the most common syntax. For the full reference, see the RE2 syntax guide.
Single-character expressions
Expression | Description | Example pattern | Example match | Doesn't match |
---|---|---|---|---|
. | Any one character, except newline | metrics/./loss | metrics/a/loss | metrics/test/loss |
[abc] | Any one of the characters a , b , c | metrics/[abc] | metrics/a | metrics/d |
[^abc] | Any one character except a , b , c | metrics/[^abc] | metrics/e | metrics/a |
\d | Any one digit | metrics/\d | metrics/3 | metrics/a |
\w | Any one word character (digit, letter, or underscore) | metrics/\w+/loss | metrics/estimated/loss | metrics/loss |
Repetitions
Expression | Description | Example pattern | Example match | Doesn't match |
---|---|---|---|---|
+ | One or more of the previous element | metrics/a+ | metrics/a , metrics/aaa | metrics/b |
? | Zero or one of the previous element | loss(_a)?/final | loss/final , loss_a/final | loss_final |
* | Zero or more of the previous element | metrics/a*/loss | metrics//loss , metrics/a/loss , metrics/aaaaa/loss | metrics/loss |
{n} | Exactly n of the previous element | loss_a{2} | loss_aa | loss_a |
Empty strings
Expression | Description | Example pattern | Matches | Doesn't match |
---|---|---|---|---|
^ | Beginning of text | ^metrics/ | metrics/loss | test/metrics/loss |
$ | End of text | loss$ | metrics/loss | metrics/loss/estimated |
\b | At ASCII word boundary | metrics\b | metrics/loss | metric/loss |