# `sep` can be a single character or a string but it will be interpreted # as part of a regexp within square brackets, so be careful if characters # such as `-` are used. create or replace function remove_comment(s, sep) as ( regexp_replace(s, '\s*[' || sep || '].*', '') ); # Examples select s, remove_comment(s, '#') as decommented, regexp_replace(decommented, ' ', '_','g') as visible from unnest( ['apples ; pears # and bananas', 'apples # pears ; and bananas'] ) _s(s);