Python3.6及更高版本的可读正则表达式。

cursive-re的Python项目详细描述


草书

Python3.6及更高版本的可读正则表达式。

安装

pip install cursive_re

示例

>>>fromcursive_reimport*>>>hash=text('#')>>>hexdigit=any_of(in_range('0','9')+in_range('a','f')+in_range('A','F'))>>>hexcolor=(...beginning_of_line()+hash+...group(repeated(hexdigit,exactly=6)|repeated(hexdigit,exactly=3))+...end_of_line()...)>>>str(hexcolor)'^\\#([a-f0-9]{6}|[a-f0-9]{3})$'>>>hexcolor_re=compile(hexcolor)re.compile('^\\#([a-f0-9]{6}|[a-f0-9]{3})$')>>>hexcolor_re.match('#fff')<re.Matchobject;span=(0,4),match='#fff'>>>>hexcolor_re.match('#ffff')isNoneTrue>>>hexcolor_re.match('#ffffff')<re.Matchobject;span=(0,7),match='#ffffff'>>>>domain_name=one_or_more(any_of(in_range('a','z')+in_range('0','9')+text('-')))>>>domain=domain_name+zero_or_more(text('.')+domain_name)>>>path_segment=zero_or_more(none_of('/'))>>>path=zero_or_more(text('/')+path_segment)>>>url=(...group(one_or_more(any_of(in_range('a','z'))),name='scheme')+text('://')+...group(domain,name='domain')+...group(path,name='path')...)>>>str(url)'(?P<scheme>[a-z]+)://(?P<domain>[a-z0-9\-]+(?:\.[a-z0-9\-]+)*)(?P<path>(?:/[^/]*)*)'

参考

cursive_re.compile

将草书表达式编译为真正的正则表达式。

cursive_re.beginning_of_line

匹配行的开头。

示例:

>>> str(beginning_of_line())
'^'

cursive_re.end_of_line

匹配行的结尾。

示例:

>>> str(end_of_line())
'$'

cursive_re.anything

匹配任何字符。

示例:

>>> str(anything())
'.'

cursive_re.literal

插入文字正则表达式。

示例:

>>> str(literal(r"\A\w"))
'\\A\\w'

cursive_re.text

与给定字符串完全匹配,转义任何特殊字符。

示例:

>>> str(text("abc"))
'abc'

cursive_re.any_of

匹配任何给定字符。

示例:

>>> str(any_of("ab"))
'[ab]'

>>> str(any_of(text("ab")))
'[ab]'

>>> str(any_of(text("[]")))
'[\\[\\]]'

cursive_re.none_of

不匹配任何给定字符。

示例:

>>> str(none_of("ab"))
'[^ab]'

>>> str(none_of(text("ab")))
'[^ab]'

>>> str(none_of(text("[]")))
'[^\\[\\]]'

cursive_re.in_range

匹配给定范围内的字符。

示例:

>>> str(in_range("a", "z"))
'a-z'

cursive_re.zero_or_more

匹配零个或多个给定表达式。

示例:

>>> str(zero_or_more("a"))
'(?:a)*'

>>> str(zero_or_more(text("a")))
'(?:a)*'

>>> str(zero_or_more(text("abc")))
'(?:abc)*'

>>> str(zero_or_more(group(text("abc"))))
'(abc)*'

cursive_re.one_or_more

匹配一个或多个给定表达式。

示例:

>>> str(one_or_more("a"))
'(?:a)+'

>>> str(one_or_more(text("a")))
'(?:a)+'

>>> str(one_or_more(group(text("abc"))))
'(abc)+'

cursive_re.maybe

匹配表达式(如果存在)。

示例:

>>> str(maybe("abc"))
'(?:abc)?'

>>> str(maybe(text("abc")))
'(?:abc)?'

>>> str(maybe(group(text("abc"))))
'(abc)?'

>>> str(maybe(any_of("abc")))
'[abc]?'

cursive_re.repeated

匹配一个表达式重复的确切次数。

示例:

>>> str(repeated("a", exactly=5))
'(?:a){5}'

>>> str(repeated(text("a"), exactly=5))
'(?:a){5}'

>>> str(repeated(text("a"), at_least=1))
'(?:a){1,}'

>>> str(repeated(text("a"), at_most=5))
'(?:a){0,5}'

>>> str(repeated(text("a"), at_least=2, at_most=5, greedy=False))
'(?:a){2,5}?'

cursive_re.group

表示其内容可以在匹配后检索的组 执行。

示例:

>>> str(group(text("a")))
'(a)'

>>> str(group(any_of("abc"), name="chars"))
'(?P<chars>[abc])'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用split函数分割字符串,但没有得到期望的结果   未找到包含derby数据库嵌入架构的sql Java桌面应用程序错误   java elasticsearch vs solr用于定制全文搜索系统   java Android:创建没有startOffset的动画延迟?   java如何查看其他应用程序接收的数据?   java如何在Linux中使用D和classpath选项运行jar文件   java和域设计最佳实践   具有相同内存位置的java数组,将显示为输出   连接到java中的elasticsearch?   Java Playframework重定向到带有Json负载的外部url   java无法在Android平台上使用InputStream为蓝牙socket创建ObjectInputStream   使用POI将Excel日期转换为Java日期,年份未正确显示   oracle从数据库层还是Java层调用webservice?