Automatically generate regular Dafa and CreateRe module instructions

Mom no longer have to worry about my regularity!

I used to write a Python module that automatically generates regular expressions, but I didn’t have a good technical solution. Suddenly this morning, a flash of light flashed, and wrote a CreateRe module in one go. To be honest, the ability to generate regularity is general. This module can generate simple regular expressions. It is suitable for beginners. It can be used by young people who don’t understand regular expressions and don’t want to learn regular rules. It is simple and convenient to generate one-click. If you have troublesome young people who want to learn regular grammar, please go to: [Python regular expression] (https://thief.one/2017/10/30/1/)

CreateRe module introduction

Python generates a regular expression module that is used to generate regular expressions in reverse. You can generate a regular expression by simply passing in the string to be matched and the list of results you expect to match.
Of course, the current module is not mature enough, the specific performance is: the first generation ability is limited (complex may not be generated), and the second is that the generated regular expression is not easy enough (we know the same matching effect, there can be many kinds of regular expressions to go Realization, experts can often write the shortest and optimal optimization rules, and this module is still in the introductory stage), only suitable for regular beginners.

CreateRe module use

Download

1
git clone https://github.com/tengzhangchao/CreateRe.git

Import Module Import Module

Import the CreateRe module, use the instance, you can refer to the test.py file in the project.

1
from CreateRe import create_re

Generating regular expressions

1
2
3
4
5
6
7
8
# to match the string
STRING = u""
S = [""]
Cur=create_re() #instantiated class
RES=cur.run(STRING,S,tag=True) #Generate regular expressions
Check_result=cur.check_res(RES,tag=True) #Check regular expressions, return matching results

run function parameter description

  • STRING string to be matched, must be in unicode format
  • S predicts a list of regular match results, must be List, and if there is a Chinese option in the list, it needs to be unicode format
  • tag greedy matching switch, the specific difference will be introduced below

Demo Description

1
2
3
4
5
6
7
8
9
10
11
12
13
14
STRING = u'''
http://thief.one nmask
http://tool.nmask.cn nm4k
http://home.nmask.cn nmask
'''
S = ["http://tool.nmask.cn"]
tag=False
cur=create_re()
RES=cur.run(STRING,S,tag=tag)
check_result=cur.check_res(RES,tag=tag)
print RES
print check_result

operation result:

1
2
([a-z] {4}: / {2} [a-z] {4} [[a-z] {5}} [a-z] [a-z]) nm4
[u'http://tool.nmask.cn']

When changing the value of the tag, tag=True, the result is:

1
2
([A-z] {4} \: / {2} [a-z] {4} \. [A-z] {5} \. [A-z] [a-z])
[u'http://tool.nmask.cn', u'http://home.nmask.cn']

Description: tag=True means to enable greedy matching, that is, the generated regular will match the result as much as possible. The default is False.

The value of S can also be specified multiple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
STRING = u'''
http://thief.one nmask
http://tool.nmask.cn nm4k
http://home.nmask.cn nmask
'''
S = ["http://tool.nmask.cn","http://thief.one"]
tag=True
cur=create_re()
RES=cur.run(STRING,S,tag=tag)
check_result=cur.check_res(RES,tag=tag)
print RES
print check_res

operation result:

1
2
([A-z] {4} \: / {2} [a-z] {4} (?:?). {2} [a-z] {3} (?:?). {4})
[u'http://thief.one', u'http://tool.nmask.cn', u'http://home.nmask.cn']

If you change the tag to False, the result is:

1
2
([A-z] {4} \: / {2} [a-z] {4} (?:?). {2} [a-z] {3} (?:?). {4})
False

Returning check_res=False indicates that the generated regular expression does not match the desired result; you can change the tag to True to try.

Get the regular expression built into the module

1
2
3
4
5
6
cur=create_re()
Print cur.get_res("email") #mailbox
Print cur.get_res("name") #name
Print cur.get_res("id_15") #ID 15 bit
Print cur.get_res("id_18") #ID 18 bit
Print cur.get_res("address") #home address

Module return value

1
2
3
4
tag=True
cur=create_re()
RES=cur.run(STRING,S,tag=tag)
check_result=cur.check_res(RES,tag=tag)
  • Regular expression generated by the RES module
  • check_result uses regular expressions to generate results that are compared to expectations. If False is returned, it means failure, and returning a result list means success.

project address

https://github.com/tengzhangchao/CreateRe

Online Regular Test

http://tool.nmask.cn/python_re/

本文标题:Automatically generate regular Dafa and CreateRe module instructions

文章作者:nmask

发布时间:2017年11月07日 - 20:11

最后更新:2019年08月16日 - 15:08

原始链接:https://thief.one/2017/11/07/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: