C*********m 发帖数: 213 | 1 向这里的高手请教一个小问题。想用python re 表达式匹配文本
[[xref:12345671|database record]][[wikipedia:AnotherLink]] test line 1
[[xref:12345672]] Test line 2
...
[[xref:12345690|another record]] test line 20
中[[xref:...]]中的内容,但是希望capture返回结果中xref:后面的数字以及|后面的
内容。
用 m = re.compile(r'\[\[xref:(\d+)(.*)\]\]')可以处理后面几行,但是第一行会把
[[wikipedia的内容也抓到一起。 | r*******n 发帖数: 3020 | 2 because .* is greedy,
so try .*?
The following from Document of Python.
*?, +?, ??
The '*', '+', and '?' qualifiers are all greedy; they match as much text
as possible. Sometimes this behaviour isn’t desired; if the RE <.*> is
matched against 'title', it will match the entire string, and not
just ''. Adding '?' after the qualifier makes it perform the match in
non-greedy or minimal fashion; as few characters as possible will be matched
. Using .*? in the previous expression will
【在 C*********m 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 向这里的高手请教一个小问题。想用python re 表达式匹配文本 : [[xref:12345671|database record]][[wikipedia:AnotherLink]] test line 1 : [[xref:12345672]] Test line 2 : ... : [[xref:12345690|another record]] test line 20 : 中[[xref:...]]中的内容,但是希望capture返回结果中xref:后面的数字以及|后面的 : 内容。 : 用 m = re.compile(r'\[\[xref:(\d+)(.*)\]\]')可以处理后面几行,但是第一行会把 : [[wikipedia的内容也抓到一起。
| C*********m 发帖数: 213 | 3 多谢. 可以了.
text
matched
【在 r*******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : because .* is greedy, : so try .*? : The following from Document of Python. : *?, +?, ?? : The '*', '+', and '?' qualifiers are all greedy; they match as much text : as possible. Sometimes this behaviour isn’t desired; if the RE <.*> is : matched against 'title', it will match the entire string, and not : just ''. Adding '?' after the qualifier makes it perform the match in : non-greedy or minimal fashion; as few characters as possible will be matched : . Using .*? in the previous expression will
|
|