我该怎么做:
if 'class="*word*"' in html:
print "True."
else:
print "False."
在Linux中使用*作为通配符?
答案 0 :(得分:1)
您将要查看re模块。这将允许您执行正则表达式并完成与*命令行中的*相同的操作。
答案 1 :(得分:0)
您可以使用re模块中的正则表达式进行通用模式匹配。
但是,如果您正在使用HTML并尝试匹配代码等,我建议您查看LXML并使用其parse
函数和cssselect
来获取您想要的内容。< / p>
from lxml import html
# read in and parse the html
html_doc = parse(filename).getroot()
# get elements that match class "classname"
elements = html_doc.cssselect(.classname)
This doc描述了不同的CSS选择器
答案 2 :(得分:0)
如果您只想匹配Unix文件名模式匹配,可以使用专用模块fnmatch:
import fnmatch
words = ["testing", "wildcard", "in", "python"]
filtered = fnmatch.filter(words, 'p?thon')
# filtered = ['python']
filtered = fnmatch.filter(words, 'w*')
# filtered = ['wildcard']
如果要进行高级模式匹配,请使用正则表达式。