简体   繁体   English

使用正则表达式跳过结果

[英]Skipping over results with regex

I want to filter out results form a API and want to exclude certain results form showing up for example:我想从 API 中过滤出结果,并希望排除显示的某些结果表单,例如:

LEGACY-NA-XboxOfficialServer219
EU-PVP-XboxOfficial-TheIslanSmallTribes219
EU-PVP-XboxOfficial-TheIsland219

I want the last result.我想要最后的结果。 So I put a Negative look behind for the smalltribes part but now need something to identify if its LEGACY or not and to skip that result if it is所以我对 smalltribes 部分进行了负面评价,但现在需要一些东西来确定它是否是 LEGACY 并跳过该结果(如果是)

What i have so far:到目前为止我所拥有的:

re.search(r"[a-zA-Z](?<!SmallTribes)" + str(number) + r"$", x['Name']):

Im trying to get it to only display我试图让它只显示

EU-PVP-XboxOfficial-TheIsland219

You may use您可以使用

^(?!LEGACY).*[a-zA-Z](?<!SmallTribes)219$

See the regex demo .请参阅正则表达式演示

Details细节

  • ^ - start of string ^ - 字符串的开始
  • (?!LEGACY) - no LEGACY substring at the start is allowed (?!LEGACY) - 不允许在开头的LEGACY子字符串
  • .* - any 0+ chars other than line break chars, as many as possible .* - 除换行符以外的任何 0+ 个字符,尽可能多
  • [a-zA-Z] - a letter [a-zA-Z] - 一个字母
  • (?<!SmallTribes) - no SmallTribes substring right before... (?<!SmallTribes) - 之前没有SmallTribes子串...
  • 219$ - the 219 number at the end of the string. 219$ - 字符串末尾的219数字。

In Python:在 Python 中:

re.search(r"^(?!LEGACY).*[a-zA-Z](?<!SmallTribes){}$".format(number), x['Name']):

See the Python demo online :在线查看Python 演示

import re
strs = ['LEGACY-NA-XboxOfficialServer219',
'EU-PVP-XboxOfficial-TheIslanSmallTribes219',
'EU-PVP-XboxOfficial-TheIsland219']
number = 219
rx = re.compile(r"^(?!LEGACY).*[a-zA-Z](?<!SmallTribes){}$".format(number))
for s in strs:
    if re.search(rx, s):
        print(s)
# => EU-PVP-XboxOfficial-TheIsland219

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM