<p>你可以用</p>
<pre class="lang-py prettyprint-override"><code>import pandas as pd
df = pd.DataFrame({'userlabel':['SZ5GZTD_[56][13631808]','YZ5GZTC-3_[51][13680735]','XZ5GZTA_12-[51][13574893]','testYZ5GZWC_11-[51][13632101]'], 'country':['russia','uk','usa','cuba']})
df['ci'] = df['userlabel'].str.extract(r"(?i)^(?:yz|testyz)[^_-]*[_-](\d+)[-_]", expand=True)
>>> df['ci']
0 NaN
1 3
2 NaN
3 11
Name: ci, dtype: object
# To rearrange columns, add the following line:
df = df[['userlabel', 'ci', 'country']]
>>> df
userlabel ci country
0 SZ5GZTD_[56][13631808] NaN russia
1 YZ5GZTC-3_[51][13680735] 3 uk
2 XZ5GZTA_12-[51][13574893] NaN usa
3 testYZ5GZWC_11-[51][13632101] 11 cuba
</code></pre>
<p><strong>请参阅<a href="https://regex101.com/r/EEvLmT/1" rel="nofollow noreferrer">regex demo</a>。</strong></p>
<p>正则表达式详细信息</em>:</p>
<ul>
<li><code>(?i)</code>-使模式不区分大小写(无需使用<code>str.lower()</code>)</li>
<li><code>^</code>-字符串的开头</li>
<li><code>(?:yz|testyz)</code>-与<code>yz</code>或<code>testyz</code>匹配的非捕获组</li>
<li><code>[^_-]*</code>-除<code>_</code>和<code>-</code>之外的零个或多个字符</li>
<li><code>[_-]</code>-第一个{<cd8>}或{<cd9>}</li>
<li><code>(\d+)</code>-group1(由于<code>Series.str.extract</code>只返回这个捕获的子字符串,所以它需要一个捕获组):一个或多个数字</li>
<li><code>[-_]</code>-a<code>-</code>或<code>_</code></李>
</ul>