Bugfix: added check for undefinedness + updated regex

Updated regular expressions & added a check for undefinedness, solving a
bug that prevented the rest of the script to run.