It isn't quite clear what you want but I think this is it. This template requires the latest version of calibre (5.40).
Code:
program:
# Isolate the first genre. If there is never more than one then use
# g = re($#genre, '\.', '/');
g = re(sublist($#genre, 0, 1, ','), '\.', '/');
# Test if the genre starts with 'fiction'.
if "^fiction" in g then
g & '/' & $authors & '/' & test($series, $series & '/', '') & $title & ' - ' & $authors
else
g & '/' & test($series, $series & '/', '') & $title & ' - ' & $authors
fi
EDIT: Rereading your post it might be that you want to check for a prefix of "fiction.", not "fiction". If so then change the "if "^fiction" line to be
Code:
if "^fiction/" in g then
NB: Use a slash because periods have already been converted to slashes, and the anchor '^' causes the check to be "starts with".