nested regular expressions in python

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 17:15:57

In python, you build regular expressions by passing a string to re.compile. You can "nest" regular expression by just doing regular string manipulation:

#!/usr/bin/env python
import re
number = 'zero | one | two | three | four | five | six | seven | eight | nine'
foo = re.compile(' quantity: \s* (%s) '%number,re.VERBOSE|re.IGNORECASE)
teststr=' quantity:    five '
print(foo.findall(teststr))
# ['five']

This is probably not quite the same. But you could do this:

import re
number = "(?:zero | one | two | three | four | five | six | seven | eight | nine)"
foo = "quantity: \s* " + number
bar = re.compile(foo, re.I | re.X)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!