擅长:python、mysql、java
<p>首先,你需要稍微调整一下。您需要将<code>\s</code>与lookbehind合并</p>
<pre><code>user_name = re.findall('(?<=[-]\s)[a-zA-Z0-9]*', text)
print(user_name)
</code></pre>
<p>但这将产生:</p>
<pre><code>['feest6811', 'kertzmann3129', '']
</code></pre>
<p>您无法将“-”作为返回列表的第三个值,因为lookback从来都不是匹配的一部分。您希望在最终<code>findall</code>匹配中使用的输入字符串中的第三个<code>-</code>位于以下上下文中:</p>
<pre><code>156.127.178.177 - [21/Jun/2019:15:45:27 -0700]
</code></pre>
<p>在本例中,返回“”的regex是一个很好的指示器,表明输入字符串中存在“-”,并且对应的空<code>[a-zA-Z0-9]*</code>子表达式匹配。如果真的让你烦恼,你可以:</p>
<pre><code>user_name = re.findall('(?<=[-]\s)[a-zA-Z0-9]*', text)
user_name = ['-' if x == '' else x for x in user_name] # convert '' to '-'
print(user_name)
</code></pre>
<p>印刷品:</p>
<pre><code>['feest6811', 'kertzmann3129', '-']
</code></pre>
<p>对于第二种用途:</p>
<pre><code>request = re.findall(r'[a-zA-Z]*\s/[a-zA-Z].*?/\d\.\d', text)
</code></pre>