yordan_yordanov

Regex

Dec 18th, 2020
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. import re
  2.  
  3. n = int(input())
  4.  
  5. for i in range(n):
  6. text = input()
  7. pattern = r"([!])(?P<command>[A-Z][a-z]{2,})(\1):(\[)(?P<message>[a-zA-Z]{7,})(\])"
  8. match = re.search(pattern, text)
  9.  
  10. if match is None:
  11. print("The message is invalid")
  12. continue
  13.  
  14. command = match.group("command")
  15. message = match.group("message")
  16.  
  17. encrypted_message = " "
  18.  
  19. for char in message:
  20. encrypted_message += str(ord(char))
  21.  
  22. print(f'{command}: {encrypted_message}')
  23.  
Advertisement
Add Comment
Please, Sign In to add comment