【发布时间】:2020-04-22 14:13:20
【问题描述】:
如何使用 re.sub python 方法将 +1 附加到电话号码?
当我使用以下函数时,它会将字符串“802-867-5309”更改为字符串“+1+15309”。我正在尝试获取此字符串“+1-802-867-5309”。文档替换中的示例显示了如何替换整个字符串我不想替换整个字符串只需附加一个 +1
import re
def transform_record(record):
new_record = re.sub("[0-9]+-","+1", record)
return new_record
print(transform_record("Some sample text 802-867-5309 some more sample text here"))
【问题讨论】:
标签: regex python-3.x