Oracle递归SQL地址替换逻辑优化:需精确匹配与忽略点两类规则
Oracle 地址标准化替换的两个版本递归CTE实现
原问题根源:原有正则的\W会匹配句点,导致不带点的替换规则会误匹配带点的地址片段(例如road.中的road会被road->Rd规则命中,替换后残留句点)。
第一版:精确匹配规则(严格校验句点,不匹配带点的待替换片段)
该版本下,仅当替换规则的匹配串完全独立、前后边界为非单词非句点字符时才触发替换,待替换片段本身带句点的不会被不带点的规则命中。
with addresses as ( select cust_id,address addr from ( select 10 cust_id,'9 Help Street, Level 4' address from dual union all select 11 cust_id,'22 Victoria Street' address from dual union all select 12 cust_id,'1495 Franklin Str.' address from dual union all select 13 cust_id,'30 Hasivim St.,Petah-Tikva' address from dual union all select 14 cust_id,'2 Jakaranda St' address from dual union all select 15 cust_id,'61, Science Park Rd' address from dual union all select 16 cust_id,'61, Social park road.' address from dual union all select 17 cust_id,'Av. Hermanos Escobar 5756' address from dual union all select 18 cust_id,'Ave. Hermanos Escobar 5756' address from dual union all select 19 cust_id,'8000 W FLORISSANT Ave.' address from dual union all select 20 cust_id,'8600 MEMORIAL PKWY SW' address from dual union all select 21 cust_id,'8200 FLORISSANTMEMORIALWAYABOVE SW' address from dual union all select 22 cust_id,'8600 MEMORIALFLORISSANT PKWY SW.' address from dual ) t1 ), replacements as ( select id,to_str,from_string from_str from ( select 1 id,'St' to_str,'Street' from_string from dual union all select 2 id,'St' to_str,'St' from_string from dual union all select 3 id,'St' to_str,'Strit' from_string from dual union all select 4 id,'St' to_str,'Str' from_string from dual union all select 5 id,'Rd' to_str,'Rd.' from_string from dual union all select 6 id,'Rd' to_str,'road' from_string from dual union all select 7 id,'Av' to_str,'Av.' from_string from dual union all select 8 id,'Av' to_str,'Ave' from_string from dual union all select 9 id,'Av' to_str,'Avenue' from_string from dual union all select 10 id,'Av' to_str,'Aven.' from_string from dual union all select 11 id,'West' to_str,'W' from_string from dual union all select 12 id,'South West' to_str,'SW' from_string from dual ) t2 ), r(cust_id,addr,test_addr,l) as ( select cust_id,addr, -- 核心修改:边界排除句点,避免匹配带点的片段 regexp_replace(addr,'(^|[^A-Za-z0-9_.])' || from_str || '($|[^A-Za-z0-9_.])','\1' || to_str || '\2') test_addr, id - 1 from addresses, replacements where id = (select count(*) from replacements) union all select cust_id,addr, -- 核心修改:边界排除句点 regexp_replace(test_addr,'(^|[^A-Za-z0-9_.])' || from_str || '($|[^A-Za-z0-9_.])','\1' || to_str || '\2') test_addr, l - 1 from r, replacements where id = l ) select cust_id,addr,test_addr from r where l=0 order by cust_id;
第一版预期效果
cust_id=16的road.、cust_id=18的Ave.、cust_id=22的SW.均保留原文本,不会被误替换- 独立不带点的
Street、road、SW等会正常触发替换
第二版:句点可选匹配(带不带点都可命中规则)
该版本下,匹配规则匹配串时允许其后跟随0或1个句点,替换时自动去掉多余句点,不管待替换片段带不带点都能命中对应规则。
with addresses as ( select cust_id,address addr from ( select 10 cust_id,'9 Help Street, Level 4' address from dual union all select 11 cust_id,'22 Victoria Street' address from dual union all select 12 cust_id,'1495 Franklin Str.' address from dual union all select 13 cust_id,'30 Hasivim St.,Petah-Tikva' address from dual union all select 14 cust_id,'2 Jakaranda St' address from dual union all select 15 cust_id,'61, Science Park Rd' address from dual union all select 16 cust_id,'61, Social park road.' address from dual union all select 17 cust_id,'Av. Hermanos Escobar 5756' address from dual union all select 18 cust_id,'Ave. Hermanos Escobar 5756' address from dual union all select 19 cust_id,'8000 W FLORISSANT Ave.' address from dual union all select 20 cust_id,'8600 MEMORIAL PKWY SW' address from dual union all select 21 cust_id,'8200 FLORISSANTMEMORIALWAYABOVE SW' address from dual union all select 22 cust_id,'8600 MEMORIALFLORISSANT PKWY SW.' address from dual ) t1 ), replacements as ( select id,to_str,from_string from_str from ( select 1 id,'St' to_str,'Street' from_string from dual union all select 2 id,'St' to_str,'St' from_string from dual union all select 3 id,'St' to_str,'Strit' from_string from dual union all select 4 id,'St' to_str,'Str' from_string from dual union all select 5 id,'Rd' to_str,'Rd.' from_string from dual union all select 6 id,'Rd' to_str,'road' from_string from dual union all select 7 id,'Av' to_str,'Av.' from_string from dual union all select 8 id,'Av' to_str,'Ave' from_string from dual union all select 9 id,'Av' to_str,'Avenue' from_string from dual union all select 10 id,'Av' to_str,'Aven.' from_string from dual union all select 11 id,'West' to_str,'W' from_string from dual union all select 12 id,'South West' to_str,'SW' from_string from dual ) t2 ), r(cust_id,addr,test_addr,l) as ( select cust_id,addr, -- 核心修改:匹配串后加可选句点 regexp_replace(addr,'(^|\W)' || from_str || '\.?(\W|$)','\1' || to_str || '\2') test_addr, id - 1 from addresses, replacements where id = (select count(*) from replacements) union all select cust_id,addr, -- 核心修改:匹配串后加可选句点 regexp_replace(test_addr,'(^|\W)' || from_str || '\.?(\W|$)','\1' || to_str || '\2') test_addr, l - 1 from r, replacements where id = l ) select cust_id,addr,test_addr from r where l=0 order by cust_id;
第二版预期效果
cust_id=16的road.会被替换为Rd、cust_id=18的Ave.会被替换为Av、cust_id=22的SW.会被替换为South West,自动去除冗余句点- 不带点的同名称片段也会正常触发替换
内容的提问来源于stack exchange,提问作者Satya V
相关产品推荐
相关产品推荐

