Python文本验证:az和逗号(",")

Evg*_*Evg 2 python regex validation

我需要检查一些文本只包含小写字母az和逗号(",").

在Python中执行此操作的最佳方法是什么?

Emi*_*ile 16

import re
def matches(s):
    return re.match("^[a-z,]*$", s) is not None
Run Code Online (Sandbox Code Playgroud)

哪个给你:

>>> matches("tea and cakes")
False
>>> matches("twiddledee,twiddledum")
True
Run Code Online (Sandbox Code Playgroud)

您可以使用re.compile优化一下:

import re
matcher = re.compile("^[a-z,]*$")
def matches(s):
    return matcher.match(s) is not None
Run Code Online (Sandbox Code Playgroud)

  • +1.我不像我的第二个解决方案那样喜欢它,但预编译版本的时间比我的解决方案快3到7倍. (4认同)

aar*_*ing 14

import string

allowed = set(string.lowercase + ',')
if set(text) - allowed:
   # you know it has forbidden characters
else:
   # it doesn't have forbidden characters 
Run Code Online (Sandbox Code Playgroud)

使用集合执行它比使用for循环更快(特别是如果你想检查多个文本)并且在这种情况下比正则表达式更清晰.

可能比两套更快的替代方案是

allowed = string.lowercase + ','
if not all(letter in allowed for letter in text):
    # you know it has forbidden characthers
Run Code Online (Sandbox Code Playgroud)

这里有一些毫无意义的mtimeit结果.one是生成器表达式,two是基于集合的解决方案.

$ python -mtimeit -s'import scratch3' 'scratch3.one("asdfas2423452345sdfadf34")'
100000 loops, best of 3: 3.98 usec per loop
$ python -mtimeit -s'import scratch3' 'scratch3.two("asdfas2423452345sdfadf34")'
100000 loops, best of 3: 4.39 usec per loop
$ python -mtimeit -s'import scratch3' 'scratch3.two("asdfasasdfadsfasdfasdfdaf")'
100000 loops, best of 3: 3.51 usec per loop
$ python -mtimeit -s'import scratch3' 'scratch3.one("asdfasasdfadsfasdfasdfdaf")'
100000 loops, best of 3: 7.7 usec per loop
Run Code Online (Sandbox Code Playgroud)

您可以看到基于setbased的表达式明显快于生成器表达式,并且具有较小的预期字母和成功条件.生成器表达式因故障而更快,因为它可以保释.这几乎是预期的,所以看到这些数字支持它很有意思.

我忘记的另一种可能性是混合方法.

not all(letter in allowed for letter in set(text))

$ python -mtimeit -s'import scratch3' 'scratch3.three("asdfasasdfadsfasdfasdfdaf")'
100000 loops, best of 3: 5.06 usec per loop
$ python -mtimeit -s'import scratch3' 'scratch3.three("asdfas2423452345sdfadf34")'
100000 loops, best of 3: 6.71 usec per loop
Run Code Online (Sandbox Code Playgroud)

它减慢了最好的情况,但加快了最糟糕的情况.总而言之,您必须测试预期输入样本的不同可能性.样本越广越好.