gpt4 book ai didi

Python正则表达式替换 anchor

转载 作者:行者123 更新时间:2023-12-01 04:29:10 25 4
gpt4 key购买 nike

我正在尝试重写我在 this 中看到的代码答案:

import re

pat1 = re.compile(r"(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)", re.IGNORECASE | re.DOTALL)

pat2 = re.compile(r"#(^|[\n ])(((www|ftp)\.[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)", re.IGNORECASE | re.DOTALL)


urlstr = 'http://www.example.com/foo/bar.html'

urlstr = pat1.sub(r'\1<a href="\2" target="_blank">\3</a>', urlstr)
urlstr = pat2.sub(r'\1<a href="http:/\2" target="_blank">\3</a>', urlstr)

print urlstr

具体来说,我尝试过这个:

pattern = re.compile('<a href="javascript:rt\(([0-9]+)\)">Download</a>');

rawtable = pattern.sub(r'\1', rawtable)

我想替换这样的东西:

<a href="javascript:rt(2061)">Download</a>

这样:

2061

我想对此做同样的事情:

<a href="#" onclick="javascript:ra('Name of object one')"
title="Some title Text">Name of Object two</a>

仅仅

Name of Object two

通过做

pattern = re.compile('<a href="#" onclick="javascript:ra\('(:?[a-zA-Z0-9 +)'\)" title="Some title Text">([a-zA-Z0-9 ]+)</a>');

rawtable = pattern.sub(r'\1', rawtable)

但它也不起作用。有什么建议吗?

最佳答案

where I want to replace something like this:

<a href="javascript:rt(2061)">Download</a>

您的第一个代码有效。 Test here


<小时/>

I'd like to do the same with this:

<a href="#" onclick="javascript:ra('Name of object one')" title="Some title Text">Name of Object two</a>`

至于第二个,请检查我在这里标记的内容:

pattern = re.compile('<a href="#" onclick="javascript:ra\('(:?[a-zA-Z0-9 +)'\)" title="Some title Text">([a-zA-Z0-9 ]+)</a>');
| | | | ^ unescaped quote (in the string passed to re.compile() )
| | | |
| | ^---------^ you didn't close the character class (as in [a-z]).. add a "]"
| ^ correct syntax is (?: pattern ) ... However, no point in using it here
^ another unescaped quote

代码:

#python 3.4.3
import re;

rawtable = '<a href="#" onclick="javascript:ra(\'Name of object one\')" title="Some title Text">Name of Object two</a>';

pattern = re.compile('<a href="#" onclick="javascript:ra\(\'[a-zA-Z0-9 ]+\'\)" title="Some title Text">([a-zA-Z0-9 ]+)</a>');

rawtable = pattern.sub(r'\1', rawtable);
print(rawtable);

Run this code

输出:

Name of Object two

关于Python正则表达式替换 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32636335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com