'Regex to remove end of string if another specific string is found first

I have a scenario with the following 3 types of values:

Team1 VS Team2 AT City
Team1 AT Team2
Team1 VS Team2

How can I create a regex that will preserve the bottom 2 but match “ AT City” in the first line?

I have tried doing some positive look behinds to see if VS exists but am struggling to find a solution that also doesn’t impact the bottom 2.



Solution 1:[1]

Here is one way to do so:

import re

data = """
Team1 VS Team2 AT City
Team1 AT Team2
Team1 VS Team2
"""

print(re.sub(r"( VS .*) AT .*", r"\1", data))

  • VS : Matches VS .
  • .*: Matches any character except line terminator, between zero and unlimited times (greedy).
  • AT : Matches AT .
  • \1: The first captured group ( VS .* )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cubix48