RosettaCodeData/Task/Strip-comments-from-a-string/DuckDB/strip-comments-from-a-string.duckdb
2025-08-11 18:05:26 -07:00

16 lines
502 B
Text

# `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);