-1

我在 python 中这样做,我有几个这样的变量。

team = "St. John's"
db_team = "St. John's"  
db_team = "St John's" 
#I am not sure which variable db_team will equal

re.search(team, db_team) 

但显然这不起作用,因为 team 变量中的 period ,但同时我不能从 team 变量中取出所有时期。不确定如何获取团队变量并匹配任一 db_team 变量?

4

3 回答 3

2

用来re.escape逃避你的点和所有其他阴暗的东西。

re.search(re.escape(team), db_team)
于 2013-06-01T23:37:06.513 回答
0
team = "St\\.? John's"

\\is 是为了逃避 the .,而is?是让它成为可选的。

于 2013-06-01T23:33:09.563 回答
0
import re
team = "St. John's"
db_team1 = "St. John's"
db_team2 = "St John's"

# find an exact match for 'St' without a dot, replace it with 'St.' 

db_team1 = re.sub(r'\bc\b', 'St.', db_team1)
db_team2 = re.sub(r'\bSt(?!\.)\b', 'St.', db_team2)
team = re.sub(r'\bSt(?!\.)\b', 'St.', team)

# then compare strings without regex

if team == db_team1: print "match1"
if team == db_team2: print "match2"

使用标准表示的相同方法可以扩展到包括其他缩写。从这个意义上说,您可以考虑首先将 db 和 user 中的所有字符串转换为小写。

于 2013-06-02T00:07:46.220 回答