首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python中的regex在开始或结束时匹配模式时出错

使用Python中的regex在开始或结束时匹配模式时出错
EN

Stack Overflow用户
提问于 2015-07-19 16:10:23
回答 2查看 164关注 0票数 1

我在Python正则表达式上遇到了困难。我想罚款任何N,S,E,W,NB,SB,EB,WB,包括在开始或结束的字符串。我的regex很容易在中间找到它,但在开始或结束时失败。

有人能建议我在代码示例下面的dirPattern i中做了什么错误吗?

注:我知道我还有其他一些问题要处理。但是我知道如何修改这些正则表达式。

提前谢谢。

代码语言:javascript
复制
import re

nameList = ['Boulder Highway and US 95 NB',  'Boulder Hwy and US 95 SB', 
'Buffalo and Summerlin N', 'Charleston and I-215 W', 'Eastern and I-215 S', 'Flamingo and NB I-15',
'S Buffalo and Summerlin', 'Flamingo and SB I-15', 'Gibson and I-215 EB', 'I-15 at 3.5 miles N of Jean',
'I-15 NB S I-215 (dual)', 'I-15 SB 4.3 mile N of Primm', 'I-15 SB S of Russell', 'I-515 SB at Eastern W',
'I-580 at I-80 N E', 'I-580 at I-80 S W', 'I-80 at E 4TH St Kietzke Ln', 'I-80 East of W McCarran',
'LV Blvd at I-215 S', 'S Buffalo and I-215 W', 'S Decatur and I-215 WB', 'Sahara and I-15 East',
'Sands and Wynn South Gate', 'Silverado Ranch and I-15 (west side)']

dirMap = {'N': 'North', 'S': 'South', 'E': 'East', 'W': 'West'}

dirPattern = re.compile(r'[ ^]([NSEW])B?[ $]')

print('name\tmatch\tdirSting\tdirection')
for name in nameList:
    match = dirPattern.search(name)
    direction = None
    dirString = None
    if match:
        dirString = match.group(1)
        if dirString in dirMap:
            direction = dirMap[dirString]
    print('%s\t%s\t%s\t%s'%(name, match, dirString, direction))

一些样本的预期产出:

名称匹配dirSting方向 Boulder高速公路和美国95 NB <_sre.SRE_Match对象在0x7f68af836648> N北部 Boulder和US 95 SB <_sre.SRE_Match在0x7f68ae836648> S南部的目标 布法罗和萨默林N <_sre.SRE_Match物体在0x7f68af826648> N北部 查尔斯顿和215号W <_sre.SRE_Match物体在0x7f68cf836648> W西部 位于北<_sre.SRE_Match的Flamingo和NB I-15目标 S水牛和Summerlin <_sre.SRE_Match对象在0x7f68aff36648> S南部 吉布森和I-215号EB <_sre.SRE_Match对象在0x7f68afa36648> E East

然而,开始或结束的例子给出:

博尔德公路和美国95 NB无一无

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-19 18:17:29

这段代码中修改过的regex完成了这一任务。这包括处理'W‘,'at E’之类的事情:

代码语言:javascript
复制
import re

nameList = ['Boulder Highway and US 95 NB',  'Boulder Hwy and US 95 SB', 
'Buffalo and Summerlin N', 'Charleston and I-215 W', 'Eastern and I-215 S', 'Flamingo and NB I-15',
'S Buffalo and Summerlin', 'Flamingo and SB I-15', 'Gibson and I-215 EB', 'I-15 at 3.5 miles N of Jean',
'I-15 NB S I-215 (dual)', 'I-15 SB 4.3 mile N of Primm', 'I-15 SB S of Russell', 'I-515 SB at Eastern W',
'I-580 at I-80 N E', 'I-580 at I-80 S W', 'I-80 at E 4TH St Kietzke Ln', 'I-80 East of W McCarran',
'LV Blvd at I-215 S', 'S Buffalo and I-215 W', 'S Decatur and I-215 WB', 'Sahara and I-15 East',
'Sands and Wynn South Gate', 'Silverado Ranch and I-15 (west side)']

dirMap = {'N': 'North', 'S': 'South', 'E': 'East', 'W': 'West'}

dirPattern = re.compile(r'(?:^| )(?<! at )(?<! of )([NSEW])B?(?! of )(?: |$)')

print('name\tdirSting\tdirection')
for name in nameList:
    match = dirPattern.search(name)
    direction = None
    dirString = None
    if match:
        dirString = match.group(1)
        direction = dirMap.get(dirString)
    print('> %s\t\t%s\t%s'%(name, dirString, direction))

regex可理解如下:

(?:^| )以字符串的开头或空格开头

(?<! at )之前没有“at”

(?<! of )之前没有“of”

([NSEW])任何一个'N','S','E','W‘(这将在match.group(1)中)

B?可选地后面跟着'B‘(如装订)

(?! of )后面没有“at”

以字符串或空格结尾的(?: |$)结尾

最后的产出是:

博尔德公路和美国95 NB北段 Boulder Hwy和美国95 SB南SB 布法罗和苏门林北部北部 查尔斯顿和215号州际公路西西 东段和南纬215号州际公路 Flamingo和NB I-15北 S水牛和Summerlin南部 弗拉明戈和SB I-15南 吉布森和215号州际公路东 1号州际公路-15号州际公路,北纬3.5英里,无人驾驶 I-15 NB S I-215 (双重)北部 普里姆南北纬15 SB 4.3英里 南拉塞尔南部15号州际公路 东西南北515号州际公路 I-580号州际公路-北纬80号州际公路 I-580号州际公路-80号州际公路西南偏南 80号州际公路E4St Kietzke无一架 80号州际公路W以东McCarran无 南纬215号州际公路左室 水牛城和南部215号州际公路 South和I-215 WB南 撒哈拉和15号州际公路东无一架 金沙和永利南门无一 Silverado牧场和I-15号州际公路(西区)无

旁注:我决定我不想要结束字符串的情况。为此,准则是:

dirPattern = re.compile(r'(?:^| )(?<! at )(?<! of )([NSEW])B? (?!of )')

票数 0
EN

Stack Overflow用户

发布于 2015-07-19 16:12:20

您需要使用围城

代码语言:javascript
复制
dirPattern = re.compile(r'(?<!\S)([NSEW])B?(?!\S)')

[ ^]将匹配空格或插入符号。(?<!\S)负查找断言匹配之前将有任何机器人,而不是一个非空间字符。(?!\S)断言,他的匹配不能后面跟着一个非空格字符。

为什么我使用负前瞻性而不是正方法,python的默认re模块将不支持(?<=^| )

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31503204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档