正则表达式与负后视、递归模式和负前瞻匹配
Regex match with negative lookbehind, recursive pattern and negative lookahead
我需要匹配这个:
void function{ { { } }}
(带平衡括号的函数定义)
但不是这个
static stTLookupTable RxTable[MAX]={
{zero, one},{zero, one},{zero, one}};
我尝试用 (?<![[=])({((?>[^{}]+|(?R))*)})(?!;)
匹配环视
但这匹配变量声明中的 {zero, one}。
(?<![[=]){((?>[^{}]+|(?R))*)}[^;]$ doesn't work either.
简而言之,我需要它来匹配函数定义,而不是数组声明,假设数组初始化以 ]=
开始。
有谁知道如何单独匹配函数定义?
PS:{((?>[^{}]+|(?R))*)}
匹配平衡括号
假设您使用的是 PyPi regex
模块,您可以使用
import regex
text = """void function{ { { } }}
static stTLookupTable RxTable[MAX]={
{zero, one},{zero, one},{zero, one}};"""
print( [x.group(3) for x in regex.finditer(r'=\s*({(?>[^{}]+|(?1))*})(*SKIP)(*F)|({((?>[^{}]+|(?2))*)})', text)] )
# => [' { { } }']
详情:
=\s*({(?>[^{}]+|(?1))*})(*SKIP)(*F)
:
=
- 一个 =
字符
\s*
- 零个或多个空格
({(?>[^{}]+|(?1))*})
- 平衡 {...}
之间的子串
(*SKIP)(*F)
- 跳过匹配并从失败位置重新开始搜索
|
- 或
({((?>[^{}]+|(?2))*)})
- 第 2 组(技术性的,用于递归):
{((?>[^{}]+|(?2))*)}
- 匹配带有平衡花括号的 {...}
子串。
您需要 return 比赛中的第 3 组。
使用 (?R)
将递归整个模式。
您可以通过匹配单词字符 \w+
或排除允许的 void function 或除 [MAX]= 之外的任何内容使用 [^\s{}=,]+
的字符并使用 PyPi 正则表达式模块递归第一个子模式 (?1)
。
\w+(?: \w+)*({(?:[^{}]++|(?1))*})
说明
\w+(?: \w+)*
匹配 {
之前的 1 个或多个单词
(
捕获 组 1
{(?:[^{}]++|(?1))*}
匹配开始和结束卷曲的递归第一个子模式(?1)
)
关闭组 1
import regex
pattern = r"\w+(?: \w+)*({(?:[^{}]++|(?1))*})"
s = ("void function{ { { } }} \n\n\n"
"static stTLookupTable RxTable[MAX]={\n"
" \n"
" {zero, one},{zero, one},{zero, one}};")
matches = regex.finditer(pattern, s)
for matchNum, match in enumerate(matches, start=1):
print (match.group())
输出
void function{ { { } }}
要删除 {...}
部分:
import regex
pattern = r"(\w+(?: \w+))({(?:[^{}]++|(?2))*})"
s = ("void function{ { { } }} \n\n\n"
"static stTLookupTable RxTable[MAX]={\n"
" \n"
" {zero, one},{zero, one},{zero, one}};")
print(regex.sub(pattern, r"", s))
再看一个python demo
我需要匹配这个:
void function{ { { } }}
(带平衡括号的函数定义) 但不是这个
static stTLookupTable RxTable[MAX]={
{zero, one},{zero, one},{zero, one}};
我尝试用 (?<![[=])({((?>[^{}]+|(?R))*)})(?!;)
匹配环视
但这匹配变量声明中的 {zero, one}。
(?<![[=]){((?>[^{}]+|(?R))*)}[^;]$ doesn't work either.
简而言之,我需要它来匹配函数定义,而不是数组声明,假设数组初始化以 ]=
开始。
有谁知道如何单独匹配函数定义?
PS:{((?>[^{}]+|(?R))*)}
匹配平衡括号
假设您使用的是 PyPi regex
模块,您可以使用
import regex
text = """void function{ { { } }}
static stTLookupTable RxTable[MAX]={
{zero, one},{zero, one},{zero, one}};"""
print( [x.group(3) for x in regex.finditer(r'=\s*({(?>[^{}]+|(?1))*})(*SKIP)(*F)|({((?>[^{}]+|(?2))*)})', text)] )
# => [' { { } }']
详情:
=\s*({(?>[^{}]+|(?1))*})(*SKIP)(*F)
:=
- 一个=
字符\s*
- 零个或多个空格({(?>[^{}]+|(?1))*})
- 平衡{...}
之间的子串
(*SKIP)(*F)
- 跳过匹配并从失败位置重新开始搜索
|
- 或({((?>[^{}]+|(?2))*)})
- 第 2 组(技术性的,用于递归):{((?>[^{}]+|(?2))*)}
- 匹配带有平衡花括号的{...}
子串。
您需要 return 比赛中的第 3 组。
使用 (?R)
将递归整个模式。
您可以通过匹配单词字符 \w+
或排除允许的 void function 或除 [MAX]= 之外的任何内容使用 [^\s{}=,]+
的字符并使用 PyPi 正则表达式模块递归第一个子模式 (?1)
。
\w+(?: \w+)*({(?:[^{}]++|(?1))*})
说明
\w+(?: \w+)*
匹配{
之前的 1 个或多个单词
(
捕获 组 1{(?:[^{}]++|(?1))*}
匹配开始和结束卷曲的递归第一个子模式(?1)
)
关闭组 1
import regex
pattern = r"\w+(?: \w+)*({(?:[^{}]++|(?1))*})"
s = ("void function{ { { } }} \n\n\n"
"static stTLookupTable RxTable[MAX]={\n"
" \n"
" {zero, one},{zero, one},{zero, one}};")
matches = regex.finditer(pattern, s)
for matchNum, match in enumerate(matches, start=1):
print (match.group())
输出
void function{ { { } }}
要删除 {...}
部分:
import regex
pattern = r"(\w+(?: \w+))({(?:[^{}]++|(?2))*})"
s = ("void function{ { { } }} \n\n\n"
"static stTLookupTable RxTable[MAX]={\n"
" \n"
" {zero, one},{zero, one},{zero, one}};")
print(regex.sub(pattern, r"", s))
再看一个python demo