正则表达式可以做到这一点吗?
Can regex do this?
之前:
=={{int:filedesc}}==
{{Information
|description = wikiwoordenboek audio
|date =
|source =
|author =
|permission =
|other_versions =
}}
[[Category:Dutch pronunciation|Example]]
是否可以找到|
和]]
之间的东西,然后用它来代替"wikiwoordenboek audio"
之后:
=={{int:filedesc}}==
{{Information
|description = Example
|date =
|source =
|author =
|permission =
|other_versions =
}}
[[Category:Dutch pronunciation|Example]]
试试这个
(\|description =\s*)([^\n]+)(.*\[\[)([^\|]+\|)([^]]+)
>>> import mwparserfromhell
>>> t = """=={{int:filedesc}}==
... {{Information
... |description = wikiwoordenboek audio
... |date =
... |source =
... |author =
... |permission =
... |other_versions =
... }}
... [[Category:Dutch pronunciation|Example]]"""
>>> for i in mwparserfromhell.parse(t).filter_templates():
... if 'information' in i.name.lower():
... i.get('description').value = 'Example'
...
>>> t
u'{{Information\n|description =Example|date =\n|source =\n|author =\n|permission =\n|other_versions =\n}}'
之前:
=={{int:filedesc}}==
{{Information
|description = wikiwoordenboek audio
|date =
|source =
|author =
|permission =
|other_versions =
}}
[[Category:Dutch pronunciation|Example]]
是否可以找到|
和]]
之间的东西,然后用它来代替"wikiwoordenboek audio"
之后:
=={{int:filedesc}}==
{{Information
|description = Example
|date =
|source =
|author =
|permission =
|other_versions =
}}
[[Category:Dutch pronunciation|Example]]
试试这个
(\|description =\s*)([^\n]+)(.*\[\[)([^\|]+\|)([^]]+)
>>> import mwparserfromhell
>>> t = """=={{int:filedesc}}==
... {{Information
... |description = wikiwoordenboek audio
... |date =
... |source =
... |author =
... |permission =
... |other_versions =
... }}
... [[Category:Dutch pronunciation|Example]]"""
>>> for i in mwparserfromhell.parse(t).filter_templates():
... if 'information' in i.name.lower():
... i.get('description').value = 'Example'
...
>>> t
u'{{Information\n|description =Example|date =\n|source =\n|author =\n|permission =\n|other_versions =\n}}'