2017-10-17 19 views
-1

パスワードチェッカーを作成したいのですが、数字、大文字、小文字、(、)、$、%、_ /以外の文字がある場合はエラーを書くことができます。Pythonの文字列内の数字、文字、特定の文字のみを許可する方法は?

import sys 
import re 
import string 
import random 

password = input("Enter Password: ") 
length = len(password) 
if length < 8: 
    print("\nPasswords must be between 8-24 characters\n\n") 
elif length > 24: 
    print ("\nPasswords must be between 8-24 characters\n\n") 

elif not re.match('[a-z]',password): 
     print ('error') 
+2

https://regexone.com/ –

+0

あなたが設定した条件にマッチする正規表現を作成する方法を求めていますか? – thumbtackthief

+1

これは非常に便利なツールです:https://regex101.com/ – thumbtackthief

答えて

0

あなたはカンマを許可したい場合は、私が言うことができない

elif not re.match('^[a-zA-Z0-9()$%_/.]*$',password):

をお試しください:私がこれまで持っているもの

。何かがうまくいかないときはPythonで

m = re.compile(r'[a-zA-Z0-9()$%_/.]*$') 
if(m.match(input_string)): 
    Do something.. 
else 
    Reject with your logic ... 
0

が使用

if re.search(r'[^a-zA-Z0-9()$%_]', password): 
    raise Exception('Valid passwords include ...(whatever)') 

この検索は、角括弧の間に定義された文字セット内の(^)ではないパスワード内の文字。

0

、あなたは例外を上げるべきである:その場合は、あなたが検証しますそれに対して正規表現を持っている必要があります^[a-zA-Z0-9()$%_/.,]*$

0

別の解決策は、次のようになります。

allowed_characters=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0','(',')','$','%','_','/'] 

password=input("enter password: ") 
if any(x not in allowed_characters for x in password): 
    print("error: invalid character") 
else: 
    print("no error") 
関連する問題