在 Python 中生成与 RegEx 匹配的字符串 [英] Generate a String that matches a RegEx in Python

查看:80
本文介绍了在 Python 中生成与 RegEx 匹配的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
在python中反转正则表达式

我想我遇到了一个听起来比实际更容易的问题......我不太确定.我想定义一个正则表达式,我想构建一些与之匹配的字符串.

是否有我可以导入的具有此功能的模块?最好不是使用 re.searchre.match 的蛮力方法.必须有一种更优雅的方式来做到这一点.

解决方案

我一直在为 使用 Python 生成随机字符串

它包含一个方法,xeger(),它允许您从正则表达式创建一个字符串:

<预><代码>>>>导入 rstr>>>rstr.xeger(r'[A-Z]\d[A-Z]\d[A-Z]\d')u'M5R 2W4'

现在,它适用于大多数基本的正则表达式.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆