首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从正则表达式模式中返回随机字符?python 3

如何从正则表达式模式中返回随机字符?python 3
EN

Stack Overflow用户
提问于 2018-09-12 06:25:19
回答 2查看 751关注 0票数 1

我想知道是否有可能以某种方式从一个正则表达式模式中返回一个随机字符,在短期内编写。

所以这是我的案例..

我创建了一些包含在Enum中的正则表达式模式:

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')

我希望将它们作为一个字符串返回,该字符串包含regex表示的所有字符,基于以下方法:

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

我已经尝试了几种方法,但我发现唯一可能的选择是使用包含长正则表达式模式的Enum,或者使用如下示例中的字符串:

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
EN

回答 2

Stack Overflow用户

发布于 2018-09-12 07:02:17

在手册的秘密模块中有一个秘诀,它可能是更好的方法:

https://docs.python.org/3.6/library/secrets.html#recipes-and-best-practices

from secrets import choice
import string
alphabet = string.ascii_letters + string.digits
while True:
    password = ''.join(choice(alphabet) for i in range(10))
    if (any(c.islower() for c in password)
        and any(c.isupper() for c in password)
            and sum(c.isdigit() for c in password) >= 3):
        break

print(password)
票数 0
EN

Stack Overflow用户

发布于 2018-09-12 08:14:36

如果您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链接:https://ideone.com/Rh4xKI。注意,LOWERCASEUPPERCASE的正则表达式需要用方括号括起来,否则它们将是由三个字符组成的文字字符串,而不是字符类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52284968

复制
相关文章

相似问题

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