完整的python语言程序如下
#!/usr/bin/python3 import re a = '<html><body><p>[<a href="/aero-desktop-wallpapers.html" title="Aero HD Wallpapers">Aero</a>, <a href="/animals-desktop-wallpapers.html" title="Animals HD Wallpapers">Animals</a>, <a href="/architecture-desktop-wallpapers.html" title="Architecture HD Wallpapers">Architecture</a>,Wallpapers">Artistic</a>, ........(省略)......... <a href="/vintage-desktop-wallpapers.html" title="Vintage HD Wallpapers">Vintage</a>]</p></body></html>'titles = re.findall('<a href="/(.*?)-desktop-wallpapers.html',str(a))print (titles) 运行结果['aero', 'animals', 'architecture', 'vintage']
python正则表达式是:
'hing'
'\wing'
'123456'
'\d\d\d\d\d\d'
'regex.py'
'.*\.py'
正则表达式(简称为 regex)是一些由字符和特殊符号组成的字符串, 描述了模式的重复或者表述多个字符。正则表达式能按照某种模式匹配一系列有相似特征的字符串。换句话说, 它们能够匹配多个字符串。
孤立的一个正则表达式并不能起到匹配字符串的作用,要让其能够匹配目标字符,需要创建一个正则表达式对象。通常向compile()函数传入一个原始字符形式的正则表达式,即 r'.....'。
要让正则表达式不区分大小写,可以向 re.compile()传入 re.IGNORECASE 或 re.I,作为第二个参数。通过传入 re.DOTALL 作为 re.compile()的第二个参数,可以让句点字符匹配所有字符,包括换行字符。
两种:1.
m = re.match(r'匹配条件', '待匹配内容')
2.
pattern = re.compile(r'匹配条件')m = pattern.match('待匹配内容')