一、匹配一行文字中的全部開頭的字母內容html
import repython
s="i love you not because of who you are, but because of who i am when i am with you"正則表達式
import re content=re.findall(r"\b\w",s) print(content)
二、匹配一行文字中的全部開頭的數字內容算法
import re數據庫
s="i love you not because 12sd 34er 56df e4 54434"設計模式
import re s="i love you not because 12sd 34er 56df e4 54434" ret=re.findall(r"\b\d+",s) print(ret)
三、匹配一行文字中的全部開頭的數字內容或字母內容123sdf服務器
s="123sdf" import re content=re.search("\w",s).group() print(s)
四、只匹配包含字母和數字的行併發
s="i love you not because\n12sd 34er 56\ndf e4 54434"框架
content=re.findall(r"\w+",s,re.M)
五、寫一個正則表達式,使其能同時識別下面全部的字符串:'bat', 'bit', 'but', 'hat', 'hit', 'hut‘機器學習
import re
s="'bat', 'bit', 'but', 'hat', 'hit', 'hut"
#方法一 s="'bat', 'bit', 'but', 'hat', 'hit', 'hut" import re content=re.findall("\w+",s) print(content) #方法二 content=re.findall("..t",s) print(content)
六、匹配全部合法的python標識符
#coding=utf-8
import re
s="awoeur awier !@# @#4_-asdf3$^&()+?><dfg$\n$"
s="awoeur awier !@# @#4_-asdf3$^&()+?><dfg$\n$" import re content=re.findall(".*",s,re.S) print(content)
七、提取每行中完整的年月日和時間字段
#coding=utf-8
import re
s="""se234 1987-02-09 07:30:00
1987-02-10 07:25:00"""
content=re.findall("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}",s) print(content)
八、將每行中的電子郵件地址替換爲你本身的電子郵件地址
#coding=utf-8
import re
s="""693152032@qq.com, werksdf@163.com, sdf@sina.com
sfjsdf@139.com, soifsdfj@134.com
pwoeir423@123.com"""
import re content=re.subn("\w+@\w+.com","test@qq.com",s) print(content,type(content))
九、匹配\home關鍵字,s="skjdfoijower \home \homewer"
s="skjdfoijower \home \homewer" import re content=re.findall(r"\\home",s) print(content)
十、使用正則提取出字符串中的單詞s="""i love you not because of who 234 you are, 234 but 3234ser because of who i am when i am with you"""
import re content=re.findall(r"\b[a-z]+",s,re.I) print(content)
十一、使用正則表達式匹配合法的郵件地址:
import re
s="""xiasd@163.com, sdlfkj@.com sdflkj@180.com solodfdsf@123.com sdlfjxiaori@139.com saldkfj.com oisdfo@.sodf.com.com"""
import re content=re.findall("\w+@\w+.com",s) print(content)
十二、去除如下html文件中的標籤,只顯示文本信息。
<
div
>
<
p
>崗位職責:</
p
>
<
p
>完成推薦算法、數據統計、接口、後臺等服務器端相關工做</
p
>
<
p
><
br
></
p
>
<
p
>必備要求:</
p
>
<
p
>良好的自我驅動力和職業素養,工做積極主動、結果導向</
p
>
<
p
> <
br
></
p
>
<
p
>技術要求:</
p
>
<
p
>一、一年以上 Python 開發經驗,掌握面向對象分析和設計,瞭解設計模式</
p
>
<
p
>二、掌握HTTP協議,熟悉MVC、MVVM等概念以及相關WEB開發框架</
p
>
<
p
>三、掌握關係數據庫開發設計,掌握 SQL,熟練使用 MySQL/PostgreSQL 中的一種<
br
></
p
>
<
p
>四、掌握NoSQL、MQ,熟練使用對應技術解決方案</
p
>
<
p
>五、熟悉 Javascript/CSS/HTML5,JQuery、React、Vue.js</
p
>
<
p
> <
br
></
p
>
<
p
>加分項:</
p
>
<
p
>大數據,數理統計,機器學習,sklearn,高性能,大併發。</
p
>
</
div
>
import re content=re.sub("</?\w+>| "," ",s) print(content)
import re
content=re.sub("</?[^>]+>"," ",s)
print(content)
1三、將如下網址提取出域名:
http://www.interoem.com/messageinfo.asp?id=35`
http://3995503.com/class/class09/news_show.asp?id=14
http://lib.wzmc.edu.cn/news/onews.asp?id=769
http://www.zy-ls.com/alfx.asp?newsid=377&id=6
http://www.fincm.com/newslist.asp?id=415
p = r"(http://.+?/).+" print(re.sub(p, lambda x : x.group(1), s2))