有没有人知道在MySQL数据库上进行模糊街道地址搜索的好脚本(或好策略)?关键问题是:
>大写(简单 – 只需使用LCASE)
>标点符号(可以使用REPLACE;不确定它是否有更高效的选项)
>缩写(这是艰难的 – street = st等)
我希望能够匹配:
123 Main st,B单元= 123 Main Street Unit b
最佳答案
虽然它并不完美且速度很慢,但您还是希望通过REGEXP()使用正则表达式.
原文链接:https://www.f2er.com/mysql/433008.html这是第一遍正则表达式,以匹配大多数情况(以及您的示例):
(?isx) # search across multiple lines and ignore case
( # full match
( # st number - what about number words like one or two?
\d+
)
\s+ # whitespace
( # street name (one or more words)
[a-z]+
(?:
\s+
[a-z]+
)*
)
\s+ # whitespace
( # street type
al(?:y\.?|ley) # aly,aly. or alley
|
ave(?:\.|nue)? # ave,ave.,or avenue
|
b(?lvd\.?|oulevard) # blvd,blvd. or boulevard
|
c(?:t\.?|ourt) # ct,ct. or court
|
cir(?:\c\.?|cle)? # cir,circ,circ. or circle
|
cres(?:\.|cent)? # cres,cres. or crescent
|
dr(?:\.|ive)? # dr,dr. or drive
|
exp(?:y\.?|ressway) # expy,expy. or expressway
|
f(?:wy\.?|reeway) # fwy,fwy. or freeway
|
g(?:rdns\.?|ardens) # grdns,grdns. or gardens
|
h(?:wy\.?|ighway) # hwy,hwy. or highway
|
l(?n\.?|ane) # ln,ln. or land
|
m(?:nr\.?|anor) # mnr,mnr. or manor
|
m(?:trwy\.?|otorway) # mtrwy,wtrwy. or motorway
|
pl(?:\.|ace)? # pl,pl. or place
|
r(?:d\.?|oad) # rd,rd. or road
|
st(?:\.|reet)? # st,st. or street
|
t(?:pk\.?|urnpike) # tpk,tpk. or turnpike
|
ter(?:\r?\.?|race) # ter,ter.,terr,terr. or terrace
|
tr(?:l.\?|ail) # trl,trl. or trail
|
pike|park|walk|loop|bay|close|gate|highlands
|
row|way|oval|dell|rise|vale|byway|lawn
)
\,? # optional comma
\s+ # whitespace
( # optional number,unit,apt or floor
(
\# # number
|
unit # unit
|
num(?:\.|ber) # num,num. or number
|
ap(?:t\.?|artment) # apt,apt. or apartment
|
fl(?:\.|oor)? # fl,fl. or floor
)
\s+
\d+
)?
)
哪个将返回:
$1 – 完全匹配
2美元 – 门牌号码
3美元 – 街道名称
4美元 – 街道类型
5美元 – 单位或贴身号码(可选)
要在MysqL中使用它,你需要删除所有注释(从’#’到eol),删除第一行(切换选项),并将所有内容折叠到一行而不包含任何空格.