Do the alternation inside the regex and choose a different regex delimiter when matching *nix paths. Also consider whether the character class \w is going to match if $fs has more slashes, eg /tmp/transient.dat, or there are spaces, eg /data/New Folder. If you really are only interested in, say, /data and not /data_files then look for slash or end of string immediately following.