在 Python 中生成与 RegEx 匹配的字符串 [英] Generate a String that matches a RegEx in Python
问题描述
可能的重复:
在python中反转正则表达式
我想我遇到了一个听起来比实际更容易的问题......我不太确定.我想定义一个正则表达式,我想构建一些与之匹配的字符串.
是否有我可以导入的具有此功能的模块?最好不是使用 re.search
或 re.match
的蛮力方法.必须有一种更优雅的方式来做到这一点.
我一直在为 使用 Python 生成随机字符串
它包含一个方法,xeger()
,它允许您从正则表达式创建一个字符串:
现在,它适用于大多数基本的正则表达式.
Possible Duplicate:
Reversing a regular expression in python
I think I ran into a problem that sounds easier than it is... I'm not too sure. I want to define a regular expression, and I want to build a number of strings matching it.
Is there any module I can import that has got this functionality? Preferably not a brute-force approach using re.search
or re.match
. There must be a more elegant way to do that.
I've been working on a little helper library for generating random strings with Python
It includes a method, xeger()
that allows you to create a string from a regex:
>>> import rstr
>>> rstr.xeger(r'[A-Z]\d[A-Z] \d[A-Z]\d')
u'M5R 2W4'
Right now, it works with most basic regular expressions.
这篇关于在 Python 中生成与 RegEx 匹配的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!