如何从正则表达式模式返回随机字符?python 3
我想知道是否有可能从一个正则表达式模式返回一个随机字符,用短期编写 这是我的案子 我创建了包含在枚举中的一些正则表达式模式:如何从正则表达式模式返回随机字符?python 3,python,regex,string,design-patterns,char,Python,Regex,String,Design Patterns,Char,我想知道是否有可能从一个正则表达式模式返回一个随机字符,用短期编写 这是我的案子 我创建了包含在枚举中的一些正则表达式模式: import random from _operator import invert from enum import Enum import re class RegexExpression(Enum): LOWERCASE = re.compile('a-z') UPPERCASE = re.compile('A-Z') DIGIT = re
import random
from _operator import invert
from enum import Enum
import re
class RegexExpression(Enum):
LOWERCASE = re.compile('a-z')
UPPERCASE = re.compile('A-Z')
DIGIT = re.compile('\d')
SYMBOLS = re.compile('\W')
我希望根据下面的方法,将其作为包含正则表达式表示的所有字符的字符串返回:
def create_password(symbol_count, digit_count, lowercase_count, uppercase_count):
pwd = ""
for i in range(1, symbol_count):
pwd.join(random.choice(invert(RegexExpression.SYMBOLS.value)))
for i in range(1, digit_count):
pwd.join(random.choice(invert(RegexExpression.DIGIT.value)))
for i in range(1, lowercase_count):
pwd.join(random.choice(invert(RegexExpression.LOWERCASE.value)))
for i in range(1, uppercase_count):
pwd.join(random.choice(invert(RegexExpression.UPPERCASE.value)))
return pwd
我尝试了几种方法,但我发现唯一可行的方法是使用包含长正则表达式模式或字符串的枚举,如以下示例所示:
LOWERCASE = "abcdefghijklmnopqrstuvwxyz"
。。。等等,还有其他正在使用的变量
对这种情况有什么建议或解决方案吗
--编辑--
疯狂的物理学家为我的问题带来了解决方案——非常感谢!
以下是工作代码:
def generate_password(length):
tmp_length = length
a = random.randint(1, length - 3)
tmp_length -= a
b = random.randint(1, length - a - 2)
tmp_length -= b
c = random.randint(1, length - a - b - 1)
tmp_length -= c
d = tmp_length
pwd = ""
for i in range(0, a):
pwd += random.choice(string.ascii_lowercase)
for i in range(0, b):
pwd += random.choice(string.ascii_uppercase)
for i in range(0, c):
pwd += random.choice(string.digits)
for i in range(0, d):
pwd += random.choice(string.punctuation)
pwd = ''.join(random.sample(pwd, len(pwd)))
return pwd
该模块具有您想要的所有定义
- 而不是
RegexExpression。使用小写
- 而不是
RegexExpression。大写
使用
- 而不是
使用RegexExpression.DIGIT
可能与RegexExpression.SYMBOLS
RegEx并不真正适合此任务。表达式用于检查字符是否属于类。我不知道有什么好方法可以在不涉及源代码/实现细节的情况下检查字符类的规范。手册的“机密”模块中有一个方法可能是更好的方法:
如果100%坚持使用正则表达式,则需要一个函数将任意字符类转换为字符串。我相信有一种更简单的方法可以做到这一点,但这里有一个通用程序:
from operator import methodcaller
from re import finditer
UNICODE_MAX = 0xFFFF
UNICODE = ''.join(map(chr, range(UNICODE_MAX + 1)))
ASCII = UNICODE [:128]
def class_contents(pattern, unicode=True, printable=True):
base = UNICODE if unicode else ASCII
result = map(methodcaller('group'), finditer(pattern, base))
if printable:
result = filter(str.isprintable, result)
return ''.join(result)
现在,您可以将此函数应用于枚举值,以获取可用字符的字符串
下面是演示结果的IDEOne链接:。请注意,
小写
和大写
的正则表达式需要用方括号括起来,否则它们将是三个文字字符串,而不是字符类。使用字符串模块?Upvote就好了。您现在有了代表:)而且,您的正则表达式中有一个错误。我已经在第二个答案中提到了这一点。
from operator import methodcaller
from re import finditer
UNICODE_MAX = 0xFFFF
UNICODE = ''.join(map(chr, range(UNICODE_MAX + 1)))
ASCII = UNICODE [:128]
def class_contents(pattern, unicode=True, printable=True):
base = UNICODE if unicode else ASCII
result = map(methodcaller('group'), finditer(pattern, base))
if printable:
result = filter(str.isprintable, result)
return ''.join(result)