...and try & catch comes to help.
Code in pastebin - http://pastebin.com/HCsDFtUv
Shortly - the function tries to write file and if success, then it deletes it and returns True. Otherwise it returns False. The function could be extended by trying the writing in specified temporary folder.
Such method allows to avoid from using specific and complex regular expressions.