reference Rule syntax

pattern-not-regex

See also
pattern-regex, pattern-not

An item of patterns. It matches a PCRE regular expression against the text of the file and drops each match of the other items that contains a match of the regex. A regex match is taken into account only when it lies entirely within the finding: one that starts or ends outside it, such as a comment later on the same line, drops nothing.

It is a negative item, like pattern-not: patterns needs a positive item next to it.

Examples

Placeholder passwords

hardcoded-password.yaml
rules:  - id: hardcoded-password    patterns:      - pattern: $NAME = "..."      - metavariable-regex:          metavariable: $NAME          regex: (?i).*password      - pattern-not-regex: (?i)example|changeme    message: hard-coded password in $NAME    languages: [python]    severity: ERROR
hardcoded-password.py
# ruleid: hardcoded-passworddb_password = "hunter2"# ok: hardcoded-passworddb_password = "changeme"# ruleid: hardcoded-passworddb_password = "hunter2"  # not an example# ok: hardcoded-passworddb_user = "admin"
Command and result
$ opengrep test .
1/1: ✓ All tests passed
No tests for fixes found.

The third assignment is reported: the word example is in the comment, outside the match.