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 <H1\>title</H1>, it will match the entire
string, and not just <H1>. 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
match only <H1>.
使用
re.search
使用
re.findall
解释:
^{} module 处理Python中的正则表达式。它的^{} 方法可用于查找字符串中模式的所有出现处。你知道吗
您感兴趣的模式的正则表达式是
'includegraphics.*?{(.*?)}'
。这里.
表示“任何字符”,而*
表示0次或更多次。问号使得这是一个非贪婪的操作。根据文件:请注意,在您的情况下,使用
.*?
应该可以,但一般来说,最好使用更专门的字符组,例如\w
表示字母数字,使用\d
表示数字,前提是您事先知道内容将由什么组成。你知道吗相关问题 更多 >
编程相关推荐