我一直在尝试将字符串与密码验证的模式进行匹配。例如,密码必须包含大小写和特殊字符。
我使用了StrCSpn (为区分大小写进行了修改)函数来扫描字符串中的字符,但我必须有一个单独的列表来查找特殊字符和大小写字母。所以代码有点笨重,https://nsis.sourceforge.io/StrCSpn,_StrCSpnReverse:_Scan_strings_for_characters
有没有办法在NSIS中使用像RegEx for at least 1 number, 1 lower case and 1 upper case letter这样的正则表达式?
发布于 2021-08-05 11:48:15
您可以使用NSISpcre plug-in
Function PasswordMatchesPolicy
Push $0
Push $1
Push $2
Push 0
Push 0
Exch 5
Push `^(?:(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*)$$` #stackoverflow.com/questions/43127814/regex-for-at-least-1-number-1-lower-case-and-1-upper-case-letter
NSISpcre::REMatches
Pop $0
${If} $0 == true
Pop $0
${For} $1 1 $0
Pop $2
${Next}
Push 1
${Else}
Push ""
${EndIf}
Exch 3
Pop $2
Pop $1
Pop $0
FunctionEnd
!include LogicLib.nsh
ShowInstDetails show
Section
!macro Test str
Push "${str}"
Call PasswordMatchesPolicy
Pop $0
${If} $0 <> 0
StrCpy $0 PASS
${Else}
StrCpy $0 FAIL
${EndIf}
DetailPrint "$0:${str}"
!macroend
!insertmacro Test "foo"
!insertmacro Test "Foo"
!insertmacro Test "Foo1"
!insertmacro Test "Foo#"
!insertmacro Test "Foo1#"
SectionEnd
或者,如果您只关心ASCII:
Function PasswordMatchesPolicy
!define /IfNDef C1_UPPER 0x0001
!define /IfNDef C1_LOWER 0x0002
!define /IfNDef C1_DIGIT 0x0004
!define /IfNDef C1_DEFINED 0x0200
System::Store S
System::Call 'KERNEL32::lstrcpyA(@r1,ms)'
StrCpy $5 ""
StrCpy $3 0
loop:
System::Call '*$1(&i$3,&i1.r4)'
${If} $4 <> 0
${If} $4 >= 65
${AndIf} $4 <= 90
IntOp $5 $5 | ${C1_UPPER}
${ElseIf} $4 >= 97
${AndIf} $4 <= 122
IntOp $5 $5 | ${C1_LOWER}
${ElseIf} $4 >= 48
${AndIf} $4 <= 57
IntOp $5 $5 | ${C1_DIGIT}
${Else}
IntOp $5 $5 | ${C1_DEFINED} ; Just mark it as "something"
${EndIf}
IntOp $3 $3 + 1
Goto loop
${EndIf}
IntOp $4 $5 & 3
${If} $4 = 3 ; C1_UPPER and C1_LOWER
${AndIf} $5 >= 4 ; Digit or symbol
Push 1
${Else}
Push 0
${EndIf}
System::Store L
FunctionEnd
https://stackoverflow.com/questions/68663202
复制相似问题