Regex 将通用正则表达式转换为bash-proof正则表达式

Regex 将通用正则表达式转换为bash-proof正则表达式,regex,bash,unix,Regex,Bash,Unix,我有以下用于检查密码策略的正则表达式。经验证,它可以工作: ^[zZ]\d{3}*$|?=.{9,}?=.[^\w\s]?=.[0-9]?=.[A-Z].[A-Z]* 我想在bash脚本中使用regex,通过以下方式验证PSS脚本: echo $password | grep "(^([zZ]\d{3})*$)|((?=.{9,})(?=.*?[^\w\s])(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*)" if [[ $? -eq 0 ]] ; then 这在bash

我有以下用于检查密码策略的正则表达式。经验证,它可以工作:

^[zZ]\d{3}*$|?=.{9,}?=.[^\w\s]?=.[0-9]?=.[A-Z].[A-Z]*

我想在bash脚本中使用regex,通过以下方式验证PSS脚本:

echo $password | grep "(^([zZ]\d{3})*$)|((?=.{9,})(?=.*?[^\w\s])(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*)"
if [[ $? -eq 0 ]] ; then
这在bash中不起作用。 我的问题是:

如何将这个纯正则表达式转换为在bash中工作的正则表达式?我需要转义哪些字符,将正则表达式传递给grep的正确方法是什么?还有什么我需要注意的吗

谢谢

这可能很困难

标准grep的功能有限。它只支持POSIX扩展正则表达式,这些表达式无法识别正则表达式所依赖的类型

如果您的机器上有GNU grep,您可以向它传递-p或-perl regexp参数,允许它使用与perl兼容的正则表达式。那么你的正则表达式就可以工作了

正如我在评论中提到的,regex As不适合passwort验证。它允许像z000这样的密码,甚至是空字符串:

(                 # Either match and capture...
 ^                #  Start of the string
 (                #  Match (and capture, uselessly in this case)
  [zZ]            #  case-insensitive z
  \d{3}           #  three digits
 )*               #  zero(!) or more times
 $                #  until the end of the string
)                 # End of first group.
|                 # OR
(                 # match and capture...
  (?=.{9,})       #  a string that's at least 9 characters long,
  (?=.*?[^\w\s])  #  contains at least one non-alnum, non-space character,
  (?=.*?[0-9])    #  at least one ASCII digit
  (?=.*?[A-Z])    #  at least one ASCII uppercase letter
  .*?[a-z].*      #  and at least one ASCII lowercase letter
)                 # no anchor to start/end of string...
更好地利用

^(?=.{9})(?=.*?[^\w\s])(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*$
这可能很困难

标准grep的功能有限。它只支持POSIX扩展正则表达式,这些表达式无法识别正则表达式所依赖的类型

如果您的机器上有GNU grep,您可以向它传递-p或-perl regexp参数,允许它使用与perl兼容的正则表达式。那么你的正则表达式就可以工作了

正如我在评论中提到的,regex As不适合passwort验证。它允许像z000这样的密码,甚至是空字符串:

(                 # Either match and capture...
 ^                #  Start of the string
 (                #  Match (and capture, uselessly in this case)
  [zZ]            #  case-insensitive z
  \d{3}           #  three digits
 )*               #  zero(!) or more times
 $                #  until the end of the string
)                 # End of first group.
|                 # OR
(                 # match and capture...
  (?=.{9,})       #  a string that's at least 9 characters long,
  (?=.*?[^\w\s])  #  contains at least one non-alnum, non-space character,
  (?=.*?[0-9])    #  at least one ASCII digit
  (?=.*?[A-Z])    #  at least one ASCII uppercase letter
  .*?[a-z].*      #  and at least one ASCII lowercase letter
)                 # no anchor to start/end of string...
更好地利用

^(?=.{9})(?=.*?[^\w\s])(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*$

谢谢如果我运行perl表单bash会更容易吗?另外,我需要对正则表达式做哪些修改?我仍然需要逃离特殊角色,对吗?非常感谢你的见解。谢谢。如果我运行perl表单bash会更容易吗?另外,我需要对正则表达式做哪些修改?我仍然需要避开特殊字符,对吗?非常感谢您的见解。与您的问题无关:这是一个奇怪的密码策略。一组有效的密码以z开头,其后正好有三位数字?我的手机可以在几秒钟内破解这个问题:@TimPietzcker我不是正则表达式专家,但这至少应该与策略匹配9个字符,4个字符类别中至少有一个字符是按字母大小写的;数字,符号。但是你是对的,我应该纠正第一部分更糟糕的是:即使是空字符串也可以作为密码使用!我将对我的答案进行分析。与您的问题无关:这是一个奇怪的密码策略。一组有效的密码以z开头,其后正好有三位数字?我的手机可以在几秒钟内破解这个问题:@TimPietzcker我不是正则表达式专家,但这至少应该与策略匹配9个字符,4个字符类别中至少有一个字符是按字母大小写的;数字,符号。但是你是对的,我应该纠正第一部分更糟糕的是:即使是空字符串也可以作为密码使用!我将对我的答案进行分析。