我在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)
但显然这不起作用,因为团队变量内部的时间段,但同时我不能从团队变量中取出所有时期。不确定如何获取团队变量并匹配db_team变量?
答案 0 :(得分:2)
使用re.escape
来逃避你的点和所有其他阴暗的东西。
re.search(re.escape(team), db_team)
答案 1 :(得分:0)
team = "St\\.? John's"
\\
是为了逃避.
而?
是为了让它成为可选项。
答案 2 :(得分: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"
使用标准表示的相同方法可以扩展为包括其他缩写。从这个意义上讲,您可以考虑将数据库中的所有字符串和用户首先转换为小写字母。