Guillaume Caillé
Guillaume Caillé

Reputation: 393

Need help regex optional whitespace before negative lookahead

I am currently building a regex to find all passwords in files. The current regex is :

(?:pass)(?:word)?(?:[\ ]{0,1})(?:[:=;,]{1})(?:[\ ]{0,1})(?!function)(.[^\s]*)

The problem is I don't want lines containing "function" after the optional whitespace.

password = potato123! found, is ok
$dbpass=train123; = found, is ok
pass:function($pass); = not found, is ok
$dbpassword= function('dontfindme'); = found, should not be found

See it in action: https://regex101.com/r/zT1kI3/35

Thank you!

Guillaume

Upvotes: 0

Views: 734

Answers (2)

MohaMad
MohaMad

Reputation: 2855

Does this regex match to your other samples?

(?:pass|password)\s*[:=;,]\s*(?!\s{0,}function)(\S*)

Demo : https://regex101.com/r/PXWw6j/1

Upvotes: 0

James Thorpe
James Thorpe

Reputation: 32202

The simplest solution is to move the look for the optional whitespace to be part of the check for function:

(?:pass)(?:word)?(?:[\ ]{0,1})(?:[:=;,]{1})(?![\ ]{0,1}function)(.[^\s]*)

Updated regex101

Upvotes: 1

Related Questions