【问题标题】:How to return a random character from a regex pattern? python 3如何从正则表达式模式返回随机字符?蟒蛇 3
【发布时间】:2018-09-11 22:25:18
【问题描述】:

我想知道是否有可能从正则表达式模式中返回单个随机字符,用短期编写。

这就是我的情况..

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

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"

... 以此类推,其他变量正在使用中。

对这种情况有什么建议或解决方案吗?

--编辑--

Mad Physicist 为我的问题带来了解决方案 - 非常感谢! 这是工作代码:

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

【问题讨论】:

  • 使用字符串模块?
  • 点赞就好了。你现在得到了代表:)
  • 另外,您的正则表达式有错误。我已经在第二个答案中解决了这个问题。

标签: python regex string design-patterns char


【解决方案1】:

string 模块有你想要的所有定义。

RegEx 并不真正适合这项任务。表达式用于检查字符是否属于某个类。我不知道在不了解源代码/实现细节的情况下检查字符类规范的好方法。

【讨论】:

    【解决方案2】:

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

    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)
    

    【讨论】:

      【解决方案3】:

      如果你 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 的正则表达式需要用方括号括起来,否则它们将是三字符字符串,而不是字符类。

      【讨论】:

        猜你喜欢
        • 2011-06-08
        • 2021-12-08
        • 2022-07-06
        • 1970-01-01
        • 1970-01-01
        • 2018-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多