简体   繁体   English

匹配python正则表达式中的复杂表达式

[英]Matching a complex expression in python regex

I have to create a unique textual marker in my document using Python 2.7, with the following function: 我必须使用Python 2.7在文档中创建一个唯一的文本标记,并具有以下功能:

def build_textual_marker(number, id):
    return "[xxxcixxx[[_'" + str(number) + "'] [_'" + id + "']]xxxcixxx]"

the output looks like this : [xxxcixxx[[_'1'] [_'24']]xxxcixxx] 输出看起来像这样: [xxxcixxx[[_'1'] [_'24']]xxxcixxx]

And then I have to catch any occurrence of this expression in my document. 然后,我必须在文档中捕获该表达式的所有出现。 I ended up to the following regular expression but it seems not working fine: 我结束了以下正则表达式,但似乎无法正常工作:

marker_regex = "\[xxxcixxx\[(\[_*?\])\s(\[_*?\])\]xxxcixxx\]"

I was wondering how should I write the correct regex in this case? 我想知道在这种情况下如何编写正确的正则表达式?

Try using 尝试使用

\[xxxcixxx\[\[_'.*?'\] \[_'.*?'\]\]xxxcixxx\]

Demo: http://regexr.com/3d887 演示: http//regexr.com/3d887

Rather than the lazy star, you might as well get along with a digit class directly (the function build_textual_marker takes a number parameter, doesn't it?): 除了懒惰的星星,您还可以直接与数字类相处(函数build_textual_marker具有number参数,不是吗?):

\[xxxcixxx\[(\[_'\d+'\])\s(\[_'\d+'\])\]xxxcixxx\]

See a demo on regex101.com . 参见regex101.com上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM