To solve this problem, we need to remove the words "Spot.Monster" or "Eurogamer" from any text provided, specifically within the context of Pokémon Scarlet and Violet Mystery Gift codes. The goal is to ensure that these words are removed along with any preceding non-alphanumeric characters or spaces that might be separating them from the actual codes.
Approach
- Identify the Target Words: The primary task is to identify and remove the words "Spot.Monster" and "Eurogamer" from the input text.
- Split and Trim: For each line in the text, split the line at the first occurrence of either of these target words. This helps in isolating the part of the text before the target word.
- Remove Trailing Separators: After splitting, any trailing non-alphanumeric characters (such as hyphens, spaces, colons, etc.) are trimmed to ensure the result is clean and only contains the code or relevant text.
Solution Code
python
import re
def remove_sources(text):
lines = text.split(‘\n’)
cleaned_lines = []
for line in lines:
Split on the first occurrence of either source
part_before = re.split(r'Eurogamer|Spot\.Monster', line, 1)[0]
# Remove trailing whitespace, hyphens, en dashes, em dashes, colons, slashes, parentheses
cleaned_line = re.sub(r'[\s\-–—/:()]+$', '', part_before)
cleaned_lines.append(cleaned_line)
return '\n'.join(cleaned_lines)
Example usage:
input_text = """Pokémon Scarlet and Violet Mystery Gift codes
TEAMSTAR – Eurogamer
GOLF30 – Spot.Monster
PARAD1SE – Eurogamer
THA12022CHAMP – Eurogamer
1STCHAMP – Spot.Monster"""
output_text = remove_sources(input_text)
print(output_text)
Explanation
- Splitting the Text: The text is split into individual lines to process each line separately.
- Regular Expression Split: Each line is split at the first occurrence of "Eurogamer" or "Spot.Monster" using regular expressions. This ensures that we get the part of the line before the target word.
- Trimming Non-Alphanumeric Characters: The trailing part (after removal of the target word) may contain separators like hyphens, spaces, etc. These are removed using a regular expression that matches and trims such characters from the end of the string.
- Reconstructing the Text: The cleaned lines are joined back together to form the final output text with the target words and their preceding separators removed.
This approach efficiently processes each line to ensure only the relevant code or text remains, adhering to the problem’s requirements.