<p>使用<code>re.findall</code></p>
<pre><code>>>> sample = 'includegraphics[x=2]{image.pdf}'
>>> re.findall('includegraphics.*?{(.*?)}',sample)
['image.pdf']
</code></pre>
<p>解释:</p>
<p><a href="https://docs.python.org/2/library/re.html" rel="nofollow">^{<cd2>} module</a>处理Python中的正则表达式。它的<a href="https://docs.python.org/2/library/re.html#re.findall" rel="nofollow">^{<cd3>}</a>方法可用于查找字符串中模式的所有出现处。你知道吗</p>
<p>您感兴趣的模式的正则表达式是<code>'includegraphics.*?{(.*?)}'</code>。这里<code>.</code>表示“任何字符”,而<code>*</code>表示0次或更多次。问号使得这是一个非贪婪的操作。根据文件:</p>
<blockquote>
<p>The <code>*</code>, <code>+</code>, and <code>?</code> qualifiers are all greedy; they match as much
text as possible. Sometimes this behaviour isn’t desired; if the RE
<code><.*></code> is matched against <code><H1\>title</H1></code>, it will match the entire
string, and not just <code><H1></code>. Adding <code>?</code> after the qualifier makes it
perform the match in non-greedy or minimal fashion; as few characters
as possible will be matched. Using <code>.*?</code> in the previous expression will
match only <code><H1></code>.</p>
</blockquote>
<p>请注意,在您的情况下,使用<code>.*?</code>应该可以,但一般来说,最好使用更专门的字符组,例如<code>\w</code>表示字母数字,使用<code>\d</code>表示数字,前提是您事先知道内容将由什么组成。你知道吗</p>