Skip to main content

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 Ctrl/Cmd + E key shortcut

Compound expressions

To match text against multiple expressions at once, join them with the compound operators AND, AND NOT and OR.

Search bar with a compound regular expression in the Neptune web app

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 Enter

Query builder

In the query builder for the experiments table, to match a String value against a regex:

  1. Ensure that the search bar is in Query mode.

  2. 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.

  3. Select the matches or not matches operator.

  4. 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:

Matches regex
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

ExpressionDescriptionExample patternExample matchDoesn't match
.Any one character, except newlinemetrics/./lossmetrics/a/lossmetrics/test/loss
[abc]Any one of the characters a, b, cmetrics/[abc]metrics/ametrics/d
[^abc]Any one character except a, b, cmetrics/[^abc]metrics/emetrics/a
\dAny one digitmetrics/\dmetrics/3metrics/a
\wAny one word character (digit, letter, or underscore)metrics/\w+/lossmetrics/estimated/lossmetrics/loss

Repetitions

ExpressionDescriptionExample patternExample matchDoesn't match
+One or more of the previous elementmetrics/a+metrics/a, metrics/aaametrics/b
?Zero or one of the previous elementloss(_a)?/finalloss/final, loss_a/finalloss_final
*Zero or more of the previous elementmetrics/a*/lossmetrics//loss, metrics/a/loss, metrics/aaaaa/lossmetrics/loss
{n}Exactly n of the previous elementloss_a{2}loss_aaloss_a

Empty strings

ExpressionDescriptionExample patternMatchesDoesn't match
^Beginning of text^metrics/metrics/losstest/metrics/loss
$End of textloss$metrics/lossmetrics/loss/estimated
\bAt ASCII word boundarymetrics\bmetrics/lossmetric/loss