Python to Regex. Regex to Python. The yRegex for humans.
当前项目代码仓暂无内容
以下内容由 AI 翻译,如有问题请 点此提交 issue 反馈
Crocs 库
Crocs 包引入了 yregex 的概念,这是一种使用纯 Python 类实现正则表达式模式的方法。该库中包含用于正则表达式操作符的 Python 类,这些类组合在一起可以形成您想要的模式。
使用 Python 编写正则表达式的主要优势在于可读性,以及能更好地理解您的正则表达式是如何工作的,因为在使用 Python 解释器时,正则表达式的每个部分都可以单独进行测试。
如果为了保住工作,您不得不调试下面这个正则表达式呢?😛
\*{1,3}(((a[0-9]c)\3{1,3}))((\$[a-z]\#)\2{1,3})\*{1,3}
您需要向专业人士寻求帮助。😛 一旦您的正则表达式成为 yregex,您将能够更清楚地理解发生了什么。
[tau@archlinux ~]$ yregex
>>> \*{1,3}(((a[0-9]c)\3{1,3}))((\$[a-z]\#)\2{1,3})\*{1,3}
# Regex Details.
Input: **a8ca8c$z#a8ca8ca8ca8c*
Regex: \*{1,3}(((a[0-9]c)\3{1,3}))((\$[a-z]\#)\2{1,3})\*{1,3}
Group dict: {}
Group 0: **a8ca8c$z#a8ca8ca8ca8c*
Groups: ('a8ca8c', 'a8ca8c', 'a8c', '$z#a8ca8ca8ca8c', '$z#')
Match with:
*a2ca2ca2ca2c$g#a2ca2ca2ca2ca2ca2ca2ca2ca2ca2ca2ca2c***
*a8ca8ca8c$j#a8ca8ca8ca8ca8ca8ca8ca8ca8c* **a5ca5c$u#a5ca5c***
**a8ca8ca8ca8c$e#a8ca8ca8ca8c* *a6ca6ca6c$q#a6ca6ca6ca6ca6ca6c**
**a1ca1ca1c$p#a1ca1ca1c** **a2ca2ca2c$x#a2ca2ca2c**
# Yregex/Code:
from crocs.regex import Repeat, Include, Seq, Group, GLink, Pattern
from crocs.core import RegexStr
repeat0 = Repeat('*', min=1, max=3, greedy=False)
seq0 = Seq('0', '9')
include0 = Include(seq0)
group2 = Group('a', include0, 'c')
repeat1 = Repeat(group2, min=1, max=3, greedy=False)
group1 = Group(group2, repeat1)
group0 = Group(group1)
seq1 = Seq('a', 'z')
include1 = Include(seq1)
group4 = Group('$', include1, '#')
repeat2 = Repeat(group1, min=1, max=3, greedy=False)
group3 = Group(group4, repeat2)
repeat3 = Repeat('*', min=1, max=3, greedy=False)
pattern0 = Pattern(repeat0, group0, group3, repeat3)
>>>
注意: 您还可以使用一个 Python 解释器实例。
tau@archlinux ~]$ python
>>> from crocs.xparser import xmake
>>> yregex = xmake(r'a.b')
>>> yregex.test()
Input: a'b
Regex: a.b
Group dict: {}
Group 0: a'b
Groups: ()
>>> print(yregex.mkcode())
from crocs.regex import Pattern, X
from crocs.core import RegexStr
x0 = X()
pattern0 = Pattern('a', x0, 'b')
>>>
实际实现支持大多数 Python 正则表达式特性,包括分组、命名分组、集合、前瞻、后顾等。
如果你需要实现一个正则表达式来解决问题,如下该怎么办?
问题: 匹配域名以 'br' 结尾且主机名以 'python' 开头的邮箱。邮箱名和主机名应仅由集合 [a-z] 中的字母组成。
如果你决定使用 crocs 的 yregex 方法,那么你可以在语句周围添加注释,并且可以分别测试每一个子模式。这应该能够提高你的推理能力,并可能延长开发/调试时间。
from crocs.regex import Seq, Include, Repeat, Pattern, NamedGroup, Include
# First we define how our Patterns look like.
name_letters = Include(Seq('a', 'z'))
# The regex {n,m} repeatition. The name should contains more
# than 0 chars.
name = Repeat(name_letters, 1)
# Create a named group to make it available after matching.
name = NamedGroup('name', name)
# The hostname part looks like the name except
# it starts with 'python' in the beginning,
hostname = Repeat(name_letters, 1)
hostname = NamedGroup('hostname', 'python', hostname)
# The Pattern class joins the sub patterns it forms a single one.
mail = Pattern(name, '@', hostname, '.', 'br')
mail.test()
mail.hits()
这将输出:
[tau@archlinux demo]$ python mails.py
Input: pokxntfr@pythont.br
Regex: (?P<name>[a-z]{1,})@(?P<hostname>python[a-z]{1,})\.br
Group dict: {'name': 'pokxntfr', 'hostname': 'pythont'}
Group 0: pokxntfr@pythont.br
Groups: ('pokxntfr', 'pythont')
Match with:
rn@pythonutfthab.br groex@pythonwy.br tgccu@pythonkb.br zzvy@pythontfb.br
ylego@pythonlfx.br r@pythonthxjnf.br l@pythonj.br
安装指南
本项目依赖于 eacc 库来解析正则表达式字符串,进而生成可能的匹配项。
注意: 仅支持 python3 环境。
pip install -r requirements.txt
pip install crocs
文档
注意: 在测试中已具备合理的测试覆盖率。如果您认为有改进准确性的好方法,请务必告知我!