regex.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import re
  2. import sre_constants
  3. from wbb import app
  4. from pyrogram import filters
  5. __MODULE__ = "Sed"
  6. __HELP__ = "**Usage:**\ns/foo/bar"
  7. DELIMITERS = ("/", ":", "|", "_")
  8. @app.on_message(filters.regex(r"s([{}]).*?\1.*".format("".join(DELIMITERS))), group=7)
  9. async def sed(_, message):
  10. sed_result = separate_sed(message.text)
  11. if message.reply_to_message:
  12. if message.reply_to_message.text:
  13. to_fix = message.reply_to_message.text
  14. elif message.reply_to_message.caption:
  15. to_fix = message.reply_to_message.caption
  16. else:
  17. return
  18. repl, repl_with, flags = sed_result
  19. if not repl:
  20. await message.reply_text(
  21. "You're trying to replace... " "nothing with something?"
  22. )
  23. return
  24. try:
  25. if infinite_checker(repl) == True:
  26. await message.reply_text("Nice try -_-")
  27. return
  28. if "i" in flags and "g" in flags:
  29. text = re.sub(repl, repl_with, to_fix, flags=re.I).strip()
  30. elif "i" in flags:
  31. text = re.sub(repl, repl_with, to_fix,
  32. count=1, flags=re.I).strip()
  33. elif "g" in flags:
  34. text = re.sub(repl, repl_with, to_fix).strip()
  35. else:
  36. text = re.sub(repl, repl_with, to_fix, count=1).strip()
  37. except sre_constants.error:
  38. return
  39. # empty string errors -_-
  40. if len(text) >= 4096:
  41. await message.reply_text(
  42. "The result of the sed command was too long for \
  43. telegram!"
  44. )
  45. elif text:
  46. await message.reply_to_message.reply_text(text)
  47. def infinite_checker(repl):
  48. regex = [
  49. r"\((.{1,}[\+\*]){1,}\)[\+\*].",
  50. r"[\(\[].{1,}\{\d(,)?\}[\)\]]\{\d(,)?\}",
  51. r"\(.{1,}\)\{.{1,}(,)?\}\(.*\)(\+|\* |\{.*\})",
  52. ]
  53. for match in regex:
  54. status = re.search(match, repl)
  55. if status:
  56. return True
  57. else:
  58. return False
  59. def separate_sed(sed_string):
  60. if (
  61. len(sed_string) >= 3
  62. and sed_string[1] in DELIMITERS
  63. and sed_string.count(sed_string[1]) >= 2
  64. ):
  65. delim = sed_string[1]
  66. start = counter = 2
  67. while counter < len(sed_string):
  68. if sed_string[counter] == "\\":
  69. counter += 1
  70. elif sed_string[counter] == delim:
  71. replace = sed_string[start:counter]
  72. counter += 1
  73. start = counter
  74. break
  75. counter += 1
  76. else:
  77. return None
  78. while counter < len(sed_string):
  79. if (
  80. sed_string[counter] == "\\"
  81. and counter + 1 < len(sed_string)
  82. and sed_string[counter + 1] == delim
  83. ):
  84. sed_string = sed_string[:counter] + sed_string[counter + 1:]
  85. elif sed_string[counter] == delim:
  86. replace_with = sed_string[start:counter]
  87. counter += 1
  88. break
  89. counter += 1
  90. else:
  91. return replace, sed_string[start:], ""
  92. flags = ""
  93. if counter < len(sed_string):
  94. flags = sed_string[counter:]
  95. return replace, replace_with, flags.lower()