Python - 用匹配对值替换正则表达式匹配
Python - Replace regular expression match with a matching pair value
假设我有一个与运行时可用的 5 位代码相关联的别名列表:
aliasPairs = [(12345,'bob'),(23456,'jon'),(34567,'jack'),(45678,'jill'),(89012,'steph')]
我想找一个简洁的表达方式:用匹配的别名替换行中的id,例如:
line = "hey there 12345!"
line = re.sub('\d{5}', value in the aliasPairs which matches the ID, line)
print line
应该输出:
hey there bob!
Python 专业人士如何简洁地编写枚举表达式?
感谢和欢呼!
当您对两类数据(例如五位数代码和别名)进行一对一映射时,请考虑使用字典。然后很容易访问任何特定的别名,给定它的代码:
import re
aliases = {
"12345":"bob",
"23456":"jon",
"34567":"jack",
"45678":"jill",
"89012":"steph"
}
line = "hey there 12345!"
line = re.sub('\d{5}', lambda v: aliases[v.group()], line)
print(line)
结果:
hey there bob!
如果您将在代码中直接使用这些别名(而不仅仅是从数据结构中引用),那么 Enum
是一个不错的选择1:
from enum import Enum
class Alias(Enum):
bob = 12345
jon = 23456
jack = 34567
jill = 45678
steph = 89012
然后使用 re
看起来像:
line = "hey there 12345!"
line = re.sub('\d{5}', lambda v: Alias(int(v.group()).name, line)
您还可以使用以下方法将该行为直接添加到 Alias
Enum
:
@classmethod
def sub(cls, line):
return re.sub('\d{5}', lambda v: cls(int(v.group())).name, line)
并在使用中:
Alias.sub("hey there 12345!")
当然,"bob"
应该是大写的,但是谁希望 Alias.Bob
遍及他们的代码?最好将替换文本与枚举成员名称分开,使用 aenum
2:
更容易完成这项工作
from aenum import Enum
import re
class Alias(Enum):
_init_ = 'value text'
bob = 12345, 'Bob'
jon = 23456, 'Jon'
jack = 34567, 'Jack'
jill = 45678, 'Jill'
steph = 89012, 'Steph'
@classmethod
def sub(cls, line):
return re.sub('\d{5}', lambda v: cls(int(v.group())).text, line)
Alias.sub('hey there 34567!')
1 有关标准 Enum
用法,请参阅 this answer。
2 披露:我是 Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) 库的作者。
假设我有一个与运行时可用的 5 位代码相关联的别名列表:
aliasPairs = [(12345,'bob'),(23456,'jon'),(34567,'jack'),(45678,'jill'),(89012,'steph')]
我想找一个简洁的表达方式:用匹配的别名替换行中的id,例如:
line = "hey there 12345!"
line = re.sub('\d{5}', value in the aliasPairs which matches the ID, line)
print line
应该输出:
hey there bob!
Python 专业人士如何简洁地编写枚举表达式?
感谢和欢呼!
当您对两类数据(例如五位数代码和别名)进行一对一映射时,请考虑使用字典。然后很容易访问任何特定的别名,给定它的代码:
import re
aliases = {
"12345":"bob",
"23456":"jon",
"34567":"jack",
"45678":"jill",
"89012":"steph"
}
line = "hey there 12345!"
line = re.sub('\d{5}', lambda v: aliases[v.group()], line)
print(line)
结果:
hey there bob!
如果您将在代码中直接使用这些别名(而不仅仅是从数据结构中引用),那么 Enum
是一个不错的选择1:
from enum import Enum
class Alias(Enum):
bob = 12345
jon = 23456
jack = 34567
jill = 45678
steph = 89012
然后使用 re
看起来像:
line = "hey there 12345!"
line = re.sub('\d{5}', lambda v: Alias(int(v.group()).name, line)
您还可以使用以下方法将该行为直接添加到 Alias
Enum
:
@classmethod
def sub(cls, line):
return re.sub('\d{5}', lambda v: cls(int(v.group())).name, line)
并在使用中:
Alias.sub("hey there 12345!")
当然,"bob"
应该是大写的,但是谁希望 Alias.Bob
遍及他们的代码?最好将替换文本与枚举成员名称分开,使用 aenum
2:
from aenum import Enum
import re
class Alias(Enum):
_init_ = 'value text'
bob = 12345, 'Bob'
jon = 23456, 'Jon'
jack = 34567, 'Jack'
jill = 45678, 'Jill'
steph = 89012, 'Steph'
@classmethod
def sub(cls, line):
return re.sub('\d{5}', lambda v: cls(int(v.group())).text, line)
Alias.sub('hey there 34567!')
1 有关标准 Enum
用法,请参阅 this answer。
2 披露:我是 Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) 库的作者。