使用正则表达式从Python中的字符串中提取括号
如果我们只知道“模板”,我如何从这个字符串中提取 {{template|{{template2}}|other params}} :
{{template0}}
{{template|{{template2}}|other params}}
{{template3}}
How I can extract {{template|{{template2}}|other params}} from this string if we just know "template":
{{template0}}
{{template|{{template2}}|other params}}
{{template3}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以满足您的要求:
它在“template”之后使用单词边界(
\b
),因此它不会匹配“template0”或“template3”。使用re.M
选项,因此^
和$
将匹配行的开头和结尾,而不是字符串的开头和结尾。编辑:尝试对评论中的换行符使用以下正则表达式:
无论您将换行符放在
|
之前还是之后,这都应该有效。编辑 2: 对于正则表达式问题,您必须预先指定输入内容,这一点非常重要。这是另一个与您最新评论中的文本一起使用的版本:
现在它将正确处理多个换行符,并且我在末尾添加了
}}
,以防您的匹配是行之前的最后一个括号组其他格式。现在它将正确处理多个换行符,并且我在末尾添加了
, your_string, re.M) >>> match.group() '{{template|{{template2}}|other params}}'}}
,以防您的匹配是行之前的最后一个括号组其他格式。它在“template”之后使用单词边界(
\b
),因此它不会匹配“template0”或“template3”。使用re.M
选项,因此^
和$
将匹配行的开头和结尾,而不是字符串的开头和结尾。编辑:尝试对评论中的换行符使用以下正则表达式:
无论您将换行符放在
|
之前还是之后,这都应该有效。编辑 2: 对于正则表达式问题,您必须预先指定输入内容,这一点非常重要。这是另一个与您最新评论中的文本一起使用的版本:
现在它将正确处理多个换行符,并且我在末尾添加了
}}
,以防您的匹配是行之前的最后一个括号组其他格式。无论您将换行符放在
|
之前还是之后,这都应该有效。编辑 2: 对于正则表达式问题,您必须预先指定输入内容,这一点非常重要。这是另一个与您最新评论中的文本一起使用的版本:
现在它将正确处理多个换行符,并且我在末尾添加了
, your_string, re.M) >>> match.group() '{{template|{{template2}}|other params}}'}}
,以防您的匹配是行之前的最后一个括号组其他格式。它在“template”之后使用单词边界(
\b
),因此它不会匹配“template0”或“template3”。使用re.M
选项,因此^
和$
将匹配行的开头和结尾,而不是字符串的开头和结尾。编辑:尝试对评论中的换行符使用以下正则表达式:
无论您将换行符放在
|
之前还是之后,这都应该有效。编辑 2: 对于正则表达式问题,您必须预先指定输入内容,这一点非常重要。这是另一个与您最新评论中的文本一起使用的版本:
现在它将正确处理多个换行符,并且我在末尾添加了
}}
,以防您的匹配是行之前的最后一个括号组其他格式。This should do what you want:
It uses a word boundary (
\b
) after 'template' so it will not match 'template0' or 'template3'. There.M
option is used so^
and$
will match the beginnings and ends of lines, instead of the beginning and end of the string.Edit: Try the following regex for the newline case from your comment:
This should work whether you put the newline before or after the
|
.Edit 2: It is very important with regex questions that you specify what the input can look like up front. Here is another version that works with the text from your latest comment:
Now it will handle multiple newlines correctly, and I added the
}}
at the end in case your match is the last bracketed group before lines with other formats.Now it will handle multiple newlines correctly, and I added the
, your_string, re.M) >>> match.group() '{{template|{{template2}}|other params}}'}}
at the end in case your match is the last bracketed group before lines with other formats.It uses a word boundary (
\b
) after 'template' so it will not match 'template0' or 'template3'. There.M
option is used so^
and$
will match the beginnings and ends of lines, instead of the beginning and end of the string.Edit: Try the following regex for the newline case from your comment:
This should work whether you put the newline before or after the
|
.Edit 2: It is very important with regex questions that you specify what the input can look like up front. Here is another version that works with the text from your latest comment:
Now it will handle multiple newlines correctly, and I added the
}}
at the end in case your match is the last bracketed group before lines with other formats.This should work whether you put the newline before or after the
|
.Edit 2: It is very important with regex questions that you specify what the input can look like up front. Here is another version that works with the text from your latest comment:
Now it will handle multiple newlines correctly, and I added the
, your_string, re.M) >>> match.group() '{{template|{{template2}}|other params}}'}}
at the end in case your match is the last bracketed group before lines with other formats.It uses a word boundary (
\b
) after 'template' so it will not match 'template0' or 'template3'. There.M
option is used so^
and$
will match the beginnings and ends of lines, instead of the beginning and end of the string.Edit: Try the following regex for the newline case from your comment:
This should work whether you put the newline before or after the
|
.Edit 2: It is very important with regex questions that you specify what the input can look like up front. Here is another version that works with the text from your latest comment:
Now it will handle multiple newlines correctly, and I added the
}}
at the end in case your match is the last bracketed group before lines with other formats.