Files
email-tracker/external/duckdb/test/sql/function/string/regex_replace.test
2025-10-24 19:21:19 -05:00

122 lines
2.3 KiB
SQL

# name: test/sql/function/string/regex_replace.test
# description: regex replace test
# group: [string]
statement ok
PRAGMA enable_verification
# standard replace
query T
SELECT regexp_replace('foobarbaz', 'b..', 'X')
----
fooXbaz
# global replace
query T
SELECT regexp_replace('ana ana', 'ana', 'banana', 'g')
----
banana banana
query T
SELECT regexp_replace('ANA ana', 'ana', 'banana', 'gi')
----
banana banana
# case sensitivity
query T
SELECT regexp_replace('ana', 'ana', 'banana', 'c')
----
banana
query T
SELECT regexp_replace('ANA', 'ana', 'banana', 'i')
----
banana
# literal match
query T
SELECT regexp_replace('as^/$df', '^/$', '', 'l')
----
asdf
query T
SELECT regexp_replace('as^/$df', '^/$', '')
----
as^/$df
# dot matches newline
query T
SELECT regexp_replace('hello
world', '.*', 'x', 'sg')
----
x
# the result here is a single row with a newline ('x\nx')
# this is a bit complicated to check in sqllogictest, so we use a JOIN with a count
# to verify the correct result
query T
SELECT COUNT(*) FROM (SELECT 'x
x') t1(a) JOIN (SELECT regexp_replace('hello
world', '.*', 'x', 'ng')) t2(a) USING (a)
----
1
# this also works with tables
statement ok
CREATE TABLE test(v VARCHAR);
statement ok
INSERT INTO test VALUES ('hello'), ('HELLO');
query T
SELECT regexp_replace(v, 'h.*', 'world', 'i') FROM test ORDER BY v
----
world
world
query T
SELECT regexp_replace(v, 'h.*', 'world', 'c') FROM test ORDER BY v
----
HELLO
world
# we cannot use non-constant options (currently)
statement error
SELECT regexp_replace(v, 'h.*', 'world', v) FROM test ORDER BY v
----
# throw on invalid options
statement error
SELECT regexp_replace('asdf', '.*SD.*', 'a', 'q')
----
# this used to fail as it should but lets make sure it still fails
statement error
select regexp_matches('abc', '*');
----
no argument for repetition operator: *
# this used to silently swallow the error from the invalid regex
statement error
select regexp_replace('abc', '*', 'X');
----
no argument for repetition operator: *
# make sure this also holds for non-constant case
statement ok
create table regex (s string, r string);
statement ok
insert into regex values ('abc', '*');
statement error
select regexp_matches(s, r) from regex;
----
no argument for repetition operator: *
statement error
select regexp_replace(s, r, 'X') from regex;
----
no argument for repetition operator: *