首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NSIS使用regEx进行模式匹配。密码验证

NSIS使用regEx进行模式匹配。密码验证
EN

Stack Overflow用户
提问于 2021-08-05 08:36:47
回答 1查看 114关注 0票数 0

我一直在尝试将字符串与密码验证的模式进行匹配。例如,密码必须包含大小写和特殊字符。

我使用了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这样的正则表达式?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-05 11:48:15

您可以使用NSISpcre plug-in

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68663202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档