带有递归表达式的正则表达式匹配嵌套的大括号?

编程入门 行业动态 更新时间:2024-10-26 07:38:29
本文介绍了带有递归表达式的正则表达式匹配嵌套的大括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试匹配像 sp { ...{...}... } 这样的文本,其中允许大括号嵌套.这是我目前所拥有的:

I'm trying to match text like sp { ...{...}... }, where the curly braces are allowed to nest. This is what I have so far:

my $regex = qr/ ( #save $1 sp\s+ #start Soar production ( #save $2 \{ #opening brace [^{}]* #anything but braces \} #closing brace | (?1) #or nested braces )+ #0 or more ) /x;

我只是无法让它匹配以下文本:sp { { word } }.谁能看出我的正则表达式有什么问题?

I just cannot get it to match the following text: sp { { word } }. Can anyone see what is wrong with my regex?

推荐答案

问题很多.递归位应该是:

There are numerous problems. The recursive bit should be:

( (?: \{ (?-1) \} | [^{}]+ )* )

一起:

my $regex = qr/ sp\s+ \{ ( (?: \{ (?-1) \} | [^{}]++ )* ) \} /x; print "$1\n" if 'sp { { word } }' =~ /($regex)/;

更多推荐

带有递归表达式的正则表达式匹配嵌套的大括号?

本文发布于:2023-11-03 12:23:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555124.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   嵌套   括号   表达式   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!