How I can extract {{template|{{template2}}|other params}} from this string if we just know "template":
{{template0}}
{{template|{{template2}}|other params}}
{{template3}}
This should do what you want:
>>> match = re.search(r'^{{template\b.*$', your_string, re.M)
>>> match.group()
'{{template|{{template2}}|other params}}'
It uses a word boundary ( \\b
) after 'template' so it will not match 'template0' or 'template3'. The re.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:
r'^{{template\b(?:[^}]\n+|\n+[^{]|.)*$'
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:
r'^{{template\b(?:[^}\n]\n+|\n+[^{\n]|.)*}}$'
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.