B
B
berassherald2020-08-23 08:40:16
Python
berassherald, 2020-08-23 08:40:16

How to teach a bot to isolate numbers in a specific position of a message, and send a command to PM with this number?

I'm trying to make a bot for a text game, or rather a buyer's bot.
But I can't figure out how, in principle, to write a code for isolating numbers in a message in one chat, and sending a message with them in another. I've been sitting on this case for not so long, but it's very interesting how it can be implemented.

An example of a message in the original chat:
⚖........... puts up for auction (214422): 1*Leather tunic - 900 gold, you need to isolate exactly the number in brackets, for each such message they are different every time.

An example of the final in another chat:
Buy lot - 214422

Is this possible through any functions associated with variables?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad Grigoriev, 2020-08-23
@berassherald

import re

s = "выставляет на аукцион (214422): 1*Кожаная туника - 900 золота , " \
    "нужно вычленить именно число в скобках, у каждого такого сообщения они каждый раз разные."

lot_re = re.compile(r'\((?P<lot_id>\d+)\)')
result = lot_re.search(s).groupdict()
print(result)

{'lot_id': '214422'}

D
Daria Motorina, 2020-08-23
@glaphire

It is most convenient to isolate numbers with regular expressions https://docs.python.org/3/library/re.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question