擅长:python、mysql、java
<p>一个重复捕获组将只捕获最后一个迭代。这就是为什么需要重新构造正则表达式以使用<code>re.findall</code>。在</p>
<pre><code>\s*
(?:
(?:^from\s+
( # Base (from (base) import ...)
(?:[a-zA-Z_][a-zA-Z_0-9]* # Variable name
(?:\.[a-zA-Z_][a-zA-Z_0-9]*)* # Attribute (.attr)
)
)\s+import\s+
)
|
(?:^import\s|,)\s*
)
( # Name of imported module (import (this))
(?:[a-zA-Z_][a-zA-Z_0-9]* # Variable name
(?:\.[a-zA-Z_][a-zA-Z_0-9]*)* # Attribute (.attr)
)
)
(?:
\s+as\s+
( # Variable module is imported into (import foo as bar)
(?:[a-zA-Z_][a-zA-Z_0-9]* # Variable name
(?:\.[a-zA-Z_][a-zA-Z_0-9]*)* # Attribute (.attr)
)
)
)?
\s*
(?=,|$) # Ensure there is another thing being imported or it is the end of string
</code></pre>
<p><a href="https://regex101.com/r/jL7oQ4/2" rel="nofollow">Try it on regex101.com</a></p>
<p>捕获组0将是<code>Base</code>,捕获组1将是导入模块的名称(您要查找的内容),捕获组2将是该模块所在的变量(<code>from (group 0) import (group 1) as (group 2)</code>)</p>
^{pr2}$
<pre><code>[('', 'pandas', ''), ('', 'os', ''), ('', 'sys', '')]
</code></pre>
<p>如果不关心其他两个捕获组,可以将它们删除。在</p>