Skip to Main Content
Cognex Support Hub
LogoLogo

Deep Learning Filter Grammar - Regular Expressions in Filters

The grammar components of filter expressiosn within VIDI Suite and VisionPro Deep Learning are comprised of the following : Literal Values, Built-in variables, Operators, Functions and Regular Expressions. In this article, we will go through the Regular Expression in Filters.

11/26/2024

Details

Filter Grammar - Regular Expressions in Filters

You can apply a regular expression to any filter variable that is a String data type.

You can return whether a regex matches: /<regex>/.test(<variable>)

  • /DF\d\d\d\d/.test(sample_name)// Returns true if the view's sample name matches the regex DF\d\d\d\d.

You can also use a capture string and compare the captured string to another string: /<regex(capture)>/.extract(<variable>), which returns the captured string.

  • /DF(\d\d\d\d)/.extract(sample_name) = '1234'// Captures the four digits from the regex and tests against '1234'.

Note: 

  • The examples above use the view-level variable sample_name, which evaluates to a String. If you attempt to use a String data type axis variable, those variables may evaluate to a vector of strings, since there may be multiple instances of the axis variable within a single view. In this case, the regex must be applied within the axis filter, as shown below:

    match[/FH\d\d\d\d/.test(feature_string)]// For this expression, the regex is applied for each match's feature_string. If any of the matches' feature_string match the regex, the view will be included.

    The following is an example of an invalid regex filter expression:

    /FH\d\d\d\d/.test (match/feature_string)// The expression should return true if the view includes a match with a feature_string matching the regex. However, this is invalid, because each view may have multiple matches.

  • You cannot use the sets variable in a regular expression.

Writing Filter Expressions

Filter expressions can include view-level filters where the variable refers to the entire view.

  • filename ~ 'REJECT'// Returns views where filename contains 'REJECT'.
  • count(feature) > 3// Returns views with more than three features.

Filter expressions can also include axis filters, where the individual marking or labeling for each view is evaluated.

  • feature [score > 0.5 ]// Returns views with a feature with score greater than 0.5.

You can combine view-level and axis-level filters.

  • filename ~ 'REJECT' and feature [score > 0.5 ]// Returns views where filename contains 'REJECT' and a feature scoring greater than 0.5.

You can combine filters for multiple axes.

  • feature [score > 0.5 ] and match [ score > 0.5 ]// Returns views with a feature with score greater than 0.5 and a match with a score greater than 0.5.

You can specify multiple filter conditions for an axis.

  • feature[score > 0.5 and matched ]// Returns views with a feature with a score greater than 0.5 and the feature is in a matched model.

Loading component...

Related Resources