Advertisement
here2share

# re_quotify.py

Aug 28th, 2023 (edited)
1,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. # re_quotify.py
  2.  
  3. import re
  4.  
  5. def re_quotify(text):
  6.     pattern = r'"""[^"]*"""|"[^"]*"|\'[^\']*\'|\S+|\s+|\\[ntrbf\\"\']'
  7.     matches = re.findall(pattern, text)
  8.     final = []
  9.     sss = ''
  10.     while matches:
  11.         s = matches.pop(0)
  12.         if s.startswith(('"', "'")):
  13.             if s == '""':
  14.                 s += matches.pop(0)
  15.             final += [sss, s]
  16.             sss = ''
  17.         else:
  18.             sss += s
  19.     return final + [sss]
  20.  
  21. text = 'This is "a sample" text\nwith\tmultiple\rstrings and """special characters: \', \", \\, \b, \f.)"""\n"""This is a\nmultiline\nstring."""\nThis is \'a single-quoted\' string.'
  22. text += '''\nI'M ALSO GLAD IT'S GREAT AT IGNORING APOSTROPHES.'''
  23. strings = re_quotify(text)
  24. print(strings)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement