日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python正则表达式试题_正则表达式练习题2

發布時間:2023/12/10 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python正则表达式试题_正则表达式练习题2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、匹配一行文字中的所有開頭的字母內容

#coding=utf-8

import re

s="i love you not because of who you are,but because of who i am when i am with you"

content=re.findall(r"\b\w",s)

print content

c:\Python27\Scripts>python task_test.py

[‘i‘,‘l‘,‘y‘,‘n‘,‘b‘,‘o‘,‘w‘,‘a‘,‘i‘,‘y‘]

2、匹配一行文字中的所有開頭的數字內容

s="i love you not because 12sd 34er 56df e4 54434"

content=re.findall(r"\b\d",sans-serif; background-color: #faf7ef;">c:\Python27\Scripts>python task_test.py

[‘1‘,‘3‘,‘5‘,‘5‘]

3、匹配一行文字中的所有開頭的數字內容或數字內容

>>> print re.match(r"\w+","123sdf").group()

123sdf

4、 只匹配包含字母和數字的行

s="i love you not because\n12sd 34er 56\ndf e4 54434"

content=re.findall(r"\w+",s,re.M)

5、寫一個正則表達式,使其能同時識別下面所有的字符串:‘bat‘,‘bit‘,‘but‘,‘hat‘,‘hit‘,‘hut‘

s="‘bat‘,‘hut"

content=re.findall(r"..t",sans-serif; background-color: #faf7ef;">[‘bat‘,‘hut‘]

6、匹配所有合法的python標識符

s="awoeur awier [email?protected]# @#4_-asdf3$^&()+?>

content=re.findall(r".*",re.DOTALL)

print s

awoeur awier [email?protected]# @#4_-asdf3$^&()+?>

$

[‘awoeur awier [email?protected]# @#4_-asdf3$^&()+?>

7、提取每行中完整的年月日和時間字段

s="""se234 1987-02-09 07:30:00

1987-02-10 07:25:00"""

content=re.findall(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}",sans-serif; background-color: #faf7ef;">se234 1987-02-09 07:30:00

1987-02-10 07:25:00

[‘1987-02-09 07:30:00‘,‘1987-02-10 07:25:00‘]

8、將每行中的電子郵件地址替換為你自己的電子郵件地址

content=re.sub(r"\[email?protected]\w+.com","[email?protected]",sans-serif; background-color: #faf7ef;">print "_______________________________________"

_______________________________________

9、匹配\home關鍵字:

>>> re.findall(r"\\home","skjdfoijower \home?? \homewer")

[‘\\home‘,‘\\home‘]

1、使用正則提取出字符串中的單詞

s="""i love you not because of who 234 you are,234 but 3234ser because of who i am when i am with you"""

content=re.findall(r"\b[a-zA-Z]+\b",‘of‘,‘who‘,‘are‘,‘am‘,‘when‘,‘with‘,‘you‘]

2、使用正則表達式匹配合法的郵件地址:

總結

以上是生活随笔為你收集整理的python正则表达式试题_正则表达式练习题2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。