在否定前瞻之前需要帮助正则表达式可选空格

Need help regex optional whitespace before negative lookahead

我目前正在构建一个正则表达式来查找文件中的所有密码。当前的正则表达式是:

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

问题是我不希望在可选空格后包含 "function" 的行。

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

查看实际效果: https://regex101.com/r/zT1kI3/35

谢谢!

纪尧姆

最简单的解决方案是将可选空格的查找移动到 function:

检查的一部分
(?:pass)(?:word)?(?:[\ ]{0,1})(?:[:=;,]{1})(?![\ ]{0,1}function)(.[^\s]*)

Updated regex101

此正则表达式与您的其他示例匹配吗?

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

演示:https://regex101.com/r/PXWw6j/1