x Rezosh
x Rezosh

Reputation: 11

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:

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

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

Upvotes: 1

Views: 53

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627327

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
  • .* - any 0+ chars other than line break chars, as many as possible
  • [a-zA-Z] - a letter
  • (?<!SmallTribes) - no SmallTribes substring right before...
  • 219$ - the 219 number at the end of the string.

In Python:

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

See the Python demo online:

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

Upvotes: 1

Related Questions