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:
Compact Notation (using the
?
prefix):
Verbose Notation (using the
?
prefix):
Verbose Notation with
nonCapturing
Field:
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:
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:
Compact Notation (using the
!
prefix):
Verbose Notation (using the
!
prefix):
Verbose Notation with
explicitCapturing
Field:
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:
Here:
The
Action
class is marked as non-capturing.Within the
FinData
pattern, theAction
group is explicitly captured.
Summary of Capturing 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