fork download
  1. import re
  2. p = re.compile(ur'(?:(?<!\w)\'((?:.|\n)+?\'?)(?:(?<!s)\'(?!\w)|(?<=s)\'(?!([^\']|\w\'\w)+\'(?!\w))))')
  3. test_str = u"'The Joneses' car'. Similar to the 'Kumbh melas', celebrated by the banks of the holy rivers of India. 'Hey'! you'll, I'm, he's, \nsome random text 'what if it's\nmultiline?'. 'I'm one of the persons''\n\n'the classes' hours'\n\n'the Joneses' car' guys' night out\n'two actresses' \nroles' and 'the hours' of classes'\n\n'The Joneses' car won't start'"
  4. subst = u"\"\g<1>\""
  5. result = re.sub(p, subst, test_str)
  6. print result
Success #stdin #stdout 0.02s 44680KB
stdin
Standard input is empty
stdout
"The Joneses' car". Similar to the "Kumbh melas", celebrated by the banks of the holy rivers of India. "Hey"! you'll, I'm, he's, 
some random text "what if it's
multiline?". "I'm one of the persons'"

"the classes' hours"

"the Joneses' car" guys' night out
"two actresses' 
roles" and "the hours' of classes"

"The Joneses' car won't start"