匹配Lua的“长括号"字符串语法 [英] Matching Lua's "Long bracket" string syntax

查看:724
本文介绍了匹配Lua的“长括号"字符串语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Lua编写jFlex lexer,但在设计正则表达式以匹配语言规范的特定部分时遇到了问题:

I'm writing a jFlex lexer for Lua, and I'm having problems designing a regular expression to match one particular part of the language specification:

也可以使用长括号括起来的长格式来定义文字字符串.我们将级别n的开头长括号定义为开头方括号,后跟n个等号,再后面是另一个开头方括号.因此,级别0的左括号被写成[[,级别1的左括号被写成[= [,依此类推.相似地定义了一个长括号.例如,级别4的右方括号写为] ====].长字符串以任何级别的开头长括号开始,并以相同级别的第一个结尾长括号结束.以方括号括起来的形式的文字可以运行多行,不会解释任何转义序列,并且会忽略任何其他级别的长括号.它们可以包含除适当级别的右括号之外的任何内容.

Literal strings can also be defined using a long format enclosed by long brackets. We define an opening long bracket of level n as an opening square bracket followed by n equal signs followed by another opening square bracket. So, an opening long bracket of level 0 is written as [[, an opening long bracket of level 1 is written as [=[, and so on. A closing long bracket is defined similarly; for instance, a closing long bracket of level 4 is written as ]====]. A long string starts with an opening long bracket of any level and ends at the first closing long bracket of the same level. Literals in this bracketed form can run for several lines, do not interpret any escape sequences, and ignore long brackets of any other level. They can contain anything except a closing bracket of the proper level.

简而言之,我正在尝试设计一个正则表达式,该表达式将匹配长括号,中间的字符串内容和长括号.仅当长括号和右括号的等号个数相同(可以为零或更大)时,才发生匹配.

In a nutshell, I am trying to design a regular expression that will match an opening long bracket, the string contents in between, and the closing long bracket. A match should only occur when the opening long bracket and closing long bracket have the same number of equal signs, which can be zero or more.

推荐答案

\[(=*)\[.*?\]\1\]

\ 1捕获第一个().

the \1 captures the first ().

这篇关于匹配Lua的“长括号"字符串语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆