如何使用正则表达式在python中检索数据?
我有一个字符串定义为如何使用正则表达式在python中检索数据?,python,regex,Python,Regex,我有一个字符串定义为 content = "f(1, 4, 'red', '/color/down1.html'); f(2, 5, 'green', '/color/colorpanel/down2.html'); f(3, 6, 'blue', '/color/colorpanel/colorlibrary/down3.html');" 以下是我尝试过但不起作用的代码: results = re.findall(r"f(.*?)", content) for each in
content = "f(1, 4, 'red', '/color/down1.html');
f(2, 5, 'green', '/color/colorpanel/down2.html');
f(3, 6, 'blue', '/color/colorpanel/colorlibrary/down3.html');"
以下是我尝试过但不起作用的代码:
results = re.findall(r"f(.*?)", content)
for each in results:
print each
如何使用正则表达式检索内容中的链接?谢谢。您可以在和上学习基本正则表达式 [^/]*匹配下表中不存在的单个字符 *量词-在零次和无限次之间匹配,尽可能多地匹配,根据需要返回(贪婪) /匹配字符/字面意思(区分大小写) 匹配任何字符(行终止符除外) html按字面意思匹配字符html(区分大小写) 或者,您可以提取
f()中的所有数据
[15]中的:p3=re.compile(r)(?=f\()。。(?您可以执行以下操作:
re.findall(r"f\(.*,.*,.*, '(.*)'", content)
您可以这样尝试:
import re
content = """f(1, 4, 'red', '/color/down1.html');
f(2, 5, 'green', '/color/colorpanel/down2.html');
f(3, 6, 'blue', '/color/colorpanel/colorlibrary/down3.html');"""
print re.findall(r"(\/[^']+?)'", content)
输出:
['/color/down1.html', '/color/colorpanel/down2.html', '/color/colorpanel/colorlibrary/down3.html']
正则表达式:
(\/[^']+?)
-匹配/
后接1个或多个非'
字符,直到第一次出现'
并在group1中捕获。您应该向我们展示您已经尝试过的代码和正则表达式。这是我尝试过的代码,但它不起作用。results=re.findall(r“f(.*),content)对于每个输入结果:打印每个您可能想要使用re.findall(re_模式、内容)的内容,其中re_模式是您的正则表达式。这正是我的问题。检索链接的正确模式是什么。您指的是什么链接?最后一部分是down3.html
还是整个链接?这真是一个聪明的链接。谢谢。关于p=re.compile(r'(?=/)*?(?)小男孩,我在回答中补充解释,如果你认为我的答案解决了你的问题,请考虑接受我的回答,谢谢。
re.findall(r"f\(.*,.*,.*, '(.*)'", content)
import re
content = """f(1, 4, 'red', '/color/down1.html');
f(2, 5, 'green', '/color/colorpanel/down2.html');
f(3, 6, 'blue', '/color/colorpanel/colorlibrary/down3.html');"""
print re.findall(r"(\/[^']+?)'", content)
['/color/down1.html', '/color/colorpanel/down2.html', '/color/colorpanel/colorlibrary/down3.html']