You can use srgn to search for patterns only within specific parts of a programming language's grammar (e.g., only inside Python classes). This is done by providing a language flag (like --python) followed by the syntactical element name and then the regex pattern.
Search Mode: If no actions (like replacement or casing flags) are specified, srgn enters 'search mode', which prints matching lines and line numbers.
Intersection (AND) vs. Join (OR):
- Default (AND): Multiple language scopes intersect.
srgn --python 'class' --python 'doc-strings' 'pattern' finds 'pattern' inside docstrings that are inside a class. - Join (OR): Use the
-j flag to run queries independently and join the results. srgn -j --python 'comments' --python 'doc-strings' 'pattern' finds 'pattern' if it is in a comment or a docstring.
# Search for 'age' only within Python class definitions
$ cat birds.py | srgn --python 'class' 'age'
# Search for patterns in either comments OR docstrings using -j
$ cat birds.py | srgn -j --python 'comments' --python 'doc-strings' 'bird[^s]'