本文介绍了正则表达式识别独立编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可能包含或可能不包含数字的字符串.如果有数字,它将是独立的,例如"3200 Fedex FL"或"10 Downing St",或作为名称的一部分,例如"4th ST NW","I96"或"US28".我正在寻找一个正则表达式,它将忽略独立的数字,并给我其余的字符串,但会将数字保留为名称的一部分

I've got a string that may or may not contain numbers. If there is a number, it will be standalone like '3200 Fedex FL' or '10 Downing St' or as part of a name like '4th ST NW' or 'I96' or 'US28'. I'm looking for a regex that will ignore the standalone numbers and give me the rest of the string but will keep the numbers as part of the name

尝试

Function getAddress(addr As String)
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.Pattern = "(\b[\d]+\b)"
RE.Global = True
RE.IgnoreCase = True

Set allMatches = RE.Execute(addr)

If (allMatches.Count <> 0) Then
    result = allMatches.Item(0).submatches.Item(0)
End If
getAddress = result
End Function

示例数据集

0个I64 EB MM 93
1519 KINGSCROSS RD
28 VA288 298
约翰·兰道夫RD
4700 WALMSLEY BL
保龄球绿RD/鲍勃白RD
BRUCE CT/FLORIDA AV
BUCK RD和WHITEHALL RD
DOWNTOWN EWRESSWY EB 2ND ST
HYHLAND VISTA/乔治华盛顿BL
HYHLAND VISTA/乔治华盛顿BL
HYHLAND VISTA DR/乔治华盛顿BL
I95 25 43
劳拉里奇磨坊RD/克拉伦斯RD
劳拉里奇磨坊RD/克拉伦斯RD
NOVAH HOWARD ST/SERDARY RD
旧COUVAHOUSE RD R COUVAHOUSE RD
旧COUVAHOUSE RD R COUVAHOUSE RD
林地与草原
1501 SAMS CR1
15281白头RD
1532 MARLBORO ST
16907 BRANDERS BRIDGE RD
1750 WILLIAM ST

0 I64 EB MM 93
1519 KINGSCROSS RD
28 VA288 298
JOHN RANDOLPH RD
4700 WALMSLEY BL
BOWLING GREEN RD / BOB WHITE RD
BRUCE CT /FLORIDA AV
BUCK RD AND WHITEHALL RD
DOWNTOWN EWRESSWY EB 2ND ST
HYHLAND VISTA / GEORGE WASHINGTON BL
HYHLAND VISTA / GEORGE WASHINGTON BL
HYHLAND VISTA DR / GEORGE WASHINGTON BL
I95 25 43
LAUARL RIDGE MILL RD /CLARENCE RD
LAUARL RIDGE MILL RD /CLARENCE RD
NOVAH HOWARD ST /SEMINARY RD
OLD COUVAHOUSE RD R COUVAHOUSE RD
OLD COUVAHOUSE RD R COUVAHOUSE RD
WOODLAND AND ROANOKE
1501 SAMS CR1
15281 WHITEHEAD RD
1532 MARLBORO ST
16907 BRANDERS BRIDGE RD
1750 WILLIAM ST

预期输出:
I64 EB毫米
国王大道VA288约翰·兰道夫RD
保龄球绿RD/鲍勃白RD
BRUCE CT/FLORIDA AV
BUCK RD和WHITEHALL RD
DOWNTOWN EWRESSWY EB 2ND ST
HYHLAND VISTA/乔治华盛顿BL
HYHLAND VISTA/乔治华盛顿BL
HYHLAND VISTA DR/乔治华盛顿BL
I95
劳拉里奇磨坊RD/克拉伦斯RD
劳拉里奇磨坊RD/克拉伦斯RD
NOVAH HOWARD ST/SERDARY RD
旧COUVAHOUSE RD R COUVAHOUSE RD
旧COUVAHOUSE RD R COUVAHOUSE RD
林地与草原
SAMS CR1
白头RD
MARLBORO ST
品牌桥RD
威廉ST

Expected Output:
I64 EB MM
KINGSCROSS RDVA288JOHN RANDOLPH RD
BOWLING GREEN RD / BOB WHITE RD
BRUCE CT /FLORIDA AV
BUCK RD AND WHITEHALL RD
DOWNTOWN EWRESSWY EB 2ND ST
HYHLAND VISTA / GEORGE WASHINGTON BL
HYHLAND VISTA / GEORGE WASHINGTON BL
HYHLAND VISTA DR / GEORGE WASHINGTON BL
I95
LAUARL RIDGE MILL RD /CLARENCE RD
LAUARL RIDGE MILL RD /CLARENCE RD
NOVAH HOWARD ST /SEMINARY RD
OLD COUVAHOUSE RD R COUVAHOUSE RD
OLD COUVAHOUSE RD R COUVAHOUSE RD
WOODLAND AND ROANOKE
SAMS CR1
WHITEHEAD RD
MARLBORO ST
BRANDERS BRIDGE RD
WILLIAM ST

推荐答案

使用 RegExp 对象的 Replace 方法:

Use the Replace method of the RegExp object:

RE.Global = True
RE.Pattern = "\b\d+(\s|$)"
result = RE.Replace(addr, "") ' Remove all matches from string

这篇关于正则表达式识别独立编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 11:40