Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Regex escaper

By: waterapple on Dec 19th, 2013  |  syntax: Python  |  size: 2.25 KB  |  hits: 44  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #-------------------------------------------------------------------------------
  2. # Name:        module1
  3. # Purpose:
  4. #
  5. # Author:      new
  6. #
  7. # Created:     19/12/2013
  8. # Copyright:   (c) new 2013
  9. # Licence:     <your licence>
  10. #-------------------------------------------------------------------------------
  11. #!/usr/bin/env python
  12.  
  13. def escape_for_c_sharp(raw_string):
  14.     converted_string = ""
  15.     for character in raw_string:
  16.         # Escape special characters
  17.         if character == "\\":
  18.             converted_string += "\\\\"# \\
  19.         # (
  20.         elif character == "(":
  21.             converted_string += "\\("# \(
  22.         # )
  23.         elif character == ")":
  24.             converted_string += "\\)"# \)
  25.         # [
  26.         elif character == "[":
  27.             converted_string += "\\["# \[
  28.         # ]
  29.         elif character == "]":
  30.             converted_string += "\\]"# \]
  31.         # {
  32.         elif character == "{":
  33.             converted_string += "\\{"# \{
  34.         # }
  35.         elif character == "}":
  36.             converted_string += "\\}"# \}
  37.         # .
  38.         elif character == ".":
  39.             converted_string += "\\."# \.
  40.         # +
  41.         elif character == "+":
  42.             converted_string += "\\+"# \+
  43.         # ^
  44.         elif character == "^":
  45.             converted_string += "\\^"# \^
  46.         # $
  47.         elif character == "$":
  48.             converted_string += "\\$"# \$
  49.         # |
  50.         elif character == "|":
  51.             converted_string += "\\|"# \|
  52.         # *
  53.         elif character == "*":
  54.             converted_string += "\\*"# \*
  55.         # ?
  56.         elif character == "?":
  57.             converted_string += "\\?"# \?
  58.         # Not a special character
  59.         else:
  60.             converted_string += character
  61.     return converted_string
  62.  
  63.  
  64. def read_file(path):
  65.     """grab the contents of a file"""
  66.     f = open(path, "r")
  67.     data = f.read()
  68.     f.close()
  69.     return data
  70.  
  71.  
  72. def save_file(file_name,data):
  73.     file = open(file_name, "wb")
  74.     file.write(data)
  75.     file.close()
  76.  
  77.  
  78. def main():
  79.     text_to_convert = read_file("input.txt")
  80.     converted_text = escape_for_c_sharp(text_to_convert)
  81.     save_file("output.txt", converted_text)
  82.     print converted_text
  83.  
  84. if __name__ == '__main__':
  85.     main()