Non-Capturing Groups

EXPRESSO

Expresso provides advanced control over pattern capturing through non-capturing groups and explicit capturing, allowing for clean, modular, and maintainable patterns.

Non-Capturing Groups

Non-capturing groups are used to define classes or groups that participate in pattern matching but are not included in the final output. This can be useful when certain parts of a pattern are auxiliary or should not clutter the result.

Defining Non-Capturing Classes

Non-capturing classes can be defined in three ways:

  1. Compact Notation (using the ? prefix):

'?Action': [ 'grew by', 'is growing by' ]
  1. Verbose Notation (using the ? prefix):

- class: '?Action'
  patterns: [ 'grew by', 'is growing by' ]
  1. Verbose Notation with nonCapturing Field:

- class: Action
  nonCapturing: true
  patterns: [ 'grew by', 'is growing by' ]

Behavior: If a class is marked as non-capturing, all its descendant classes will also be non-capturing unless explicitly overridden.

Defining Non-Capturing Groups in Patterns

Even if a class is not non-capturing, it can be treated as such within a specific pattern by prefixing it with ?.

Example:

Number:
  Integer: [ '\d+' ]
  Decimal: [ '${?Integer}\.${?Integer}' ]

Here:

  • The Integer class is captured normally in other contexts.

  • Within the Decimal pattern, Integer is used but will not be captured.

Explicit Capturing

Explicit capturing allows you to override non-capturing behavior at both the class and group levels.

Explicit Capturing for Classes

You can define a class as explicit capturing if it extends a non-capturing parent class. This can be done in verbose or compact notation:

  1. Compact Notation (using the ! prefix):

'?NonCapturing':
  '!Capturing': [ 'specific patterns' ]
  1. Verbose Notation (using the ! prefix):

- class: '?NonCapturing'

- class: '!Capturing'
  extends: NonCapturing
  patterns: [ 'specific patterns' ]
  1. Verbose Notation with explicitCapturing Field:

- class: NonCapturing
  nonCapturing: true

- class: Capturing
  extends: NonCapturing
  explicitCapturing: true
  patterns: [ 'specific patterns' ]

Explicit Capturing for Groups in Patterns

You can mark specific groups within a pattern as explicitly capturing even if their class is non-capturing. Use the ! prefix for this.

Example:

?Action: [ 'grew by', 'is growing by' ]
FinData: [ '${FinIndicator} ${!Action} ${Percent}' ]

Here:

  • The Action class is marked as non-capturing.

  • Within the FinData pattern, the Action group is explicitly captured.

Summary of Capturing Behavior

Type
Behavior

Non-Capturing

Excludes classes or groups from results unless explicitly captured.

Explicit Capturing

Overrides non-capturing behavior for specific classes or groups.

Group-Level Control

Allows fine-grained control within individual patterns using ? or !.

With non-capturing and explicit capturing, Expresso gives you powerful tools to build clean and structured patterns while maintaining precision in your outputs

Last updated