In this case, I would say the opposite, that is, replace everything following the # an empty line:
gsub("#.*$", "", x) [1] "gfd gdr tsvfvetrv erv tevgergre "
But can you also use an undesirable modifier ? so that your regex works as you suggested:
gsub("(.*?)#.*$", "\\1", x) [1] "gfd gdr tsvfvetrv erv tevgergre "
Andrie
source share