字符串的正则表达式

时间:2015-11-08 23:08:11

标签: python regex python-3.x lua

我希望在Python 3中为lua长字符串创建一个正则表达式。

它们应采用以下格式:

他们应该从" ["然后是0或更多" ="然后" ["再次。 接下来应该是字符串并用:"]"完成,然后是相同数量的等号和"]"试。

例如:

[[ hello world ]]

[===[ hello world ]===]

[====[ trick ]==] still ]===] in the ]========] string ]====]

如果重要的话,我正在使用python3

1 个答案:

答案 0 :(得分:3)

我很确定这就是你所要求的:

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

\[            #matches '[' literally
    (         #first capture group
        =*?   #match the smallest number of concurrent '=' signs to make this match valid
    )
\[            #matches '[' literally
    (         #second capture group (this is if you just want the string value)
        .*?   #matches the smallest number of characters to make this match valid
    )
\]            #matches ']' literally
    \1        #match an exact copy of the first capture group (makes sure both sides have the same number of equal signs)
\]            #matches ']' literally

Regex101