【解決方法】正確な単語を設定するにはどうすればよいですか?


import requests
import re

# Replace with the path to the text file

file_path = r'C:\'

# Replace with your webhook URL
webhook_url = 'https://discord.com'

# Create a set to store seen messages
seen_messages = set()

# Keep reading and sending the last line of the file
while True:
    try:
        # Read the last line of the file
        with open(file_path, 'rb') as f:
            f.seek(-2, 2)
            while f.read(1) != b'\n':
                f.seek(-2, 1)
            last_line = f.readline().decode()

        # Check if the last line contains [CLAN] and deposited, and doesn't contain vehiculul
        if 'Newbie' in last_line or 'Advanced' in last_line or 'Veteran' in last_line or 'Legend' in last_line or 'Inviter' in last_line or 'The Sentinel' in last_line or 'The King' in last_line or 'deposited' in last_line:
            # Use regular expressions to remove hex codes in the format {FF0000} from the last line
            formatted_line = re.sub(r'\{([0-9A-Fa-f]{6})\}', '', last_line)
            formatted_line = re.sub(r'\[CLAN\]', '', formatted_line)
            formatted_line = re.sub(r']', ' ', formatted_line)
            formatted_line = re.sub(r'\[', '', formatted_line)
            formatted_line = re.sub(r'»', '', formatted_line)

            # Check if the message has been seen before
            if formatted_line not in seen_messages:
                # Add the message to the set of seen messages
                seen_messages.add(formatted_line)

                # Send the message to the Discord webhook
                requests.post(webhook_url, data={'content': formatted_line})
    except:
        # Ignore any errors and continue
        pass

私が試したこと:

これがコードです。

正確な単語を使用している行のみを使用するようにしています。 例 Legend has kicked the ball- 「Legend」という単語が含まれているため使用する Legendary has kicked the ball- 「Legend」という単語が正確に含まれていないため使用しない

解決策 1

最も簡単なのは、おそらく正規表現を使用することです

参照はここにあります: re — 正規表現操作 — Python 3.11.1 ドキュメント[[^]
例を含む 2 部構成の記事: 正規表現: Python の正規表現 (パート 1) – Real Python[^]

正確な単語の一致に関するブログ: Python 正規表現で正確な単語を一致させる方法は? (回答: しないでください) – Finxter[^]

コメント

タイトルとURLをコピーしました