且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在python中嵌套正则表达式

更新时间:2023-02-21 10:46:05

在python中,通过将一个字符串传递给re.compile来构建正则表达式
您可以通过执行常规字符串操作嵌套正则表达式:

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']