我对Reply很感兴趣,因为我正在寻找一个工具,它可以推断出一个匹配字符串的正则表达式。用help
检查rexpy.extract
,看起来它“可能”是我想要的。
extract(examples, tag=False, encoding=None, as_object=False, extra_letters=None, full_escape=False, remove_empties=False, strip=False, variableLengthFrags=False, max_patterns=None, min_diff_strings_per_pattern=1, min_strings_per_pattern=1, size=None, seed=None, dialect='portable', verbose=0)
Extract regular expression(s) from examples and return them.
Normally, examples should be unicode (i.e. ``str`` in Python3,
and ``unicode`` in Python2). However, encoded strings can be
passed in provided the encoding is specified.
Results will always be unicode.
If as_object is set, the extractor object is returned,
with results in .results.rex; otherwise, a list of regular
expressions, as unicode strings is returned.
我试了一个例子:
>>> from tdda import rexpy
>>> s = 'andrew.gelman@statistics.com'
>>> rexpy.extract(s)
['^[.@]$', '^[a-z]$']
我期望类似于['^[a-z].[a-z]@[a-z].[a-z]$']
而不是['^[.@]$', '^[a-z]$']
。提取器是否只是告诉我特殊符号'.'
和'@'
在字符串中的“某处”使用?
1条答案
按热度按时间tkclm6bt1#
examples
参数期望字符串的可迭代性,通过提供单个字符串作为参数,函数迭代每个字符并输出正则表达式以匹配这些单个字符的示例。尝试提供字符串列表,例如。
rexpy.extract([s])
。