fork download
  1. import re
  2. import ast
  3.  
  4. test = ("[ \"A\", \"\"B\"\",'C' , \" D\"]\n"
  5. "[ \"A\", \"'B'\",'C' , \" D\"]\n"
  6. "[ \"A\", ''B'','C' , \" D\"]\n"
  7. "[ \"A\", '\"B\"','C' , \" D\"]\n"
  8. "[ \"A\", '8 o'clock','C' , \" D\"]\n"
  9. "[ \"A\", \"Ol' 8 o'clock\",'C' , \" D\"]\n"
  10. "[\"Some Text\"]\n"
  11. "[\"Some more Text\"]\n"
  12. "[\"Even more text about \\\"this text\\\"\"]\n"
  13. "[\"Ol' 8 o'clock\"]\n"
  14. "['8 o'clock']\n"
  15. "[ '8 o'clock']\n"
  16. "['Ol' 8 o'clock']\n"
  17. "[\"\"B\"]\n"
  18. "[\"\\\"B\"]\n"
  19. "[\"\\\\\"B\"]\n"
  20. "[\"\\\\\\\"B\"]\n"
  21. "[\"\\\\\\\\\"B\"]")
  22. result = u''
  23. last_index = 0
  24. regex1 = r"(?<=[\[,])\s*(['\"])(?:(\1)|.)*?\1(?=\s*[,\]])" #nested quotes of the same type
  25. regex2 = r'''^"[^"\\]*(?:\\.[^\"\\]*)*"$|^'[^'\\]*(?:\\.[^'\\]*)*'$|^\s*["'][^"'\\]*|["']\s*$|\\.|(["'])[^"'\\\n]*''' # unescaped quotes in $1
  26. matches = re.finditer(regex1, test, re.MULTILINE)
  27. for match in matches:
  28. if match.groups()[1] is not None: #nested quotes of the same type present
  29. print(match.group())
  30. inner = re.finditer(regex2, match.group())
  31. for m in inner:
  32. if m.groups()[0] is not None: # unescaped quotes in $1 present
  33. result += test[last_index:match.start() + m.start()] + '\\' + m.group()
  34. last_index = match.start()+m.end()
  35. result += test[last_index:len(test)]
  36. print(result)
  37.  
  38. for test_str in result.split("\n"):
  39. List = ast.literal_eval(test_str)
  40. print(List)
  41. print(type(List))
Success #stdin #stdout 0.05s 10028KB
stdin
Standard input is empty
stdout
 ""B""
 ''B''
 '8 o'clock'
"Even more text about \"this text\""
'8 o'clock'
 '8 o'clock'
'Ol' 8 o'clock'
""B"
"\"B"
"\\"B"
"\\\"B"
"\\\\"B"
[ "A", "\"B\"",'C' , " D"]
[ "A", "'B'",'C' , " D"]
[ "A", '\'B\'','C' , " D"]
[ "A", '"B"','C' , " D"]
[ "A", '8 o\'clock','C' , " D"]
[ "A", "Ol' 8 o'clock",'C' , " D"]
["Some Text"]
["Some more Text"]
["Even more text about \"this text\""]
["Ol' 8 o'clock"]
['8 o\'clock']
[ '8 o\'clock']
['Ol\' 8 o\'clock']
["\"B"]
["\"B"]
["\\\"B"]
["\\\"B"]
["\\\\\"B"]
['A', '"B"', 'C', ' D']
<class 'list'>
['A', "'B'", 'C', ' D']
<class 'list'>
['A', "'B'", 'C', ' D']
<class 'list'>
['A', '"B"', 'C', ' D']
<class 'list'>
['A', "8 o'clock", 'C', ' D']
<class 'list'>
['A', "Ol' 8 o'clock", 'C', ' D']
<class 'list'>
['Some Text']
<class 'list'>
['Some more Text']
<class 'list'>
['Even more text about "this text"']
<class 'list'>
["Ol' 8 o'clock"]
<class 'list'>
["8 o'clock"]
<class 'list'>
["8 o'clock"]
<class 'list'>
["Ol' 8 o'clock"]
<class 'list'>
['"B']
<class 'list'>
['"B']
<class 'list'>
['\\"B']
<class 'list'>
['\\"B']
<class 'list'>
['\\\\"B']
<class 'list'>