Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重新定义正则表达式(python)_Python_Regex - Fatal编程技术网

重新定义正则表达式(python)

重新定义正则表达式(python),python,regex,Python,Regex,我有很长的弦。像这样 {{ name = name prodcuer =producer writer = writer language = {{english}} country = USA }} Here is text I Need This paragraph.; 我不想要{{}之间的文本。我尝试了regex,但没有找到任何运气。有人能帮我吗 这里:是一个示例正则表达式,它匹配{{}中的整个文本 您可以使用re.sub轻松清理字符串 请注意,当您有

我有很长的弦。像这样

{{ name = name
    prodcuer =producer
    writer = writer 
    language = {{english}}
    country = USA 
}}
Here is text I Need This paragraph.;
我不想要
{{
}
之间的文本。我尝试了
regex
,但没有找到任何运气。有人能帮我吗

这里:是一个示例正则表达式,它匹配
{{}
中的整个文本

您可以使用
re.sub
轻松清理字符串

请注意,当您有像
{{test}}段落{{{test}}
这样的文本时,它将不起作用,因为它将匹配整个字符串。

正如拉斯曼所说,如果你能套上牙套,事情会变得更难。那么这就是某种类型的工作

编辑:

用法如下:

>>> text = """
... {{ name = name
...     prodcuer =producer
...     writer = writer 
...     language = {{english}}
...     country = USA 
... }}
... Here is text I Need This paragraph.;
... """
>>> import re
>>> exp = "\{\{.+\}\}"
>>> re.sub(exp, "", text, flags=re.S | re.I).strip()
'Here is text I Need This paragraph.;'

这看起来像某种模板文件。Python有多个可用的模板引擎,如果您使用一个允许
{{
}
标记替换文本的模板引擎,您可以使用模板引擎将整个内容替换为空字符串


您可能已经在使用某种模板引擎,因为您已经有了该文本。你能用这个引擎来摆脱
{{{}}
吗?

如果
{}
可以嵌套到任意深度,那么这不是一个有限状态/regexp问题?更多的例子也会有帮助。@Dejw我只需要最后一个paragraph@DmitryBeransky这是我的代码
re.search(r'(?ms)。*?{{(.*?}}),text)
正确吗
text=re.search(r'\{.+\}',text)