python 回复:需要用网站链接替换数字
python re: Need to replace numbers with links to websites
我有这样的字符串:
this is SBN: 1245897 some text some text SBN4589364 some text some
text SBN-7859157, some text some text Part Num: 2615, some text SBN:7859148 asasas
YYY-485 some text some text...
我只需要获取所有与 SBN 相关的号码并将其替换为某些站点的链接,我正在尝试使用此代码,但这不起作用
p = re.compile('SBN[:- ] ( \d+ )')
p.sub(r'<a href="http://example.com/?sbn=" target="_blank"></a>', string)
请帮忙..
正则表达式中的所有空格都很重要!仅在您希望在匹配中包含空格时才包含空格。您可以将其限制为仅包含空格 (
),但通常 'all whitespace' 更有用 (\s
)。
我还建议始终将正则表达式编写为原始字符串。
此正则表达式捕获您问题中的所有示例:
p = re.compile(r'SBN[:-]\s*(\d+)')
我有这样的字符串:
this is SBN: 1245897 some text some text SBN4589364 some text some text SBN-7859157, some text some text Part Num: 2615, some text SBN:7859148 asasas YYY-485 some text some text...
我只需要获取所有与 SBN 相关的号码并将其替换为某些站点的链接,我正在尝试使用此代码,但这不起作用
p = re.compile('SBN[:- ] ( \d+ )')
p.sub(r'<a href="http://example.com/?sbn=" target="_blank"></a>', string)
请帮忙..
正则表达式中的所有空格都很重要!仅在您希望在匹配中包含空格时才包含空格。您可以将其限制为仅包含空格 (
),但通常 'all whitespace' 更有用 (\s
)。
我还建议始终将正则表达式编写为原始字符串。
此正则表达式捕获您问题中的所有示例:
p = re.compile(r'SBN[:-]\s*(\d+)')