python

您所在的位置:网站首页 s开头的中文字 python

python

#python| 来源: 网络整理| 查看: 265

 

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', 'y', 'a', 'b', 'b', 'o', 'w', 'i', 'a', 'w', 'i', 'a', 'w', 'y']

 

2、匹配一行文字中的所有开头的数字内容

 

import re

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

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

print content

c:\Python27\Scripts>python task_test.py

['1', '3', '5', '5']

3、匹配一行文字中的所有开头的数字内容或数字内容

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

123sdf

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

#coding=utf-8

 

import re

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

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

print content

c:\Python27\Scripts>python task_test.py

['i', 'love', 'you', 'not', 'because', '12sd', '34er', '56', 'df', 'e4', '54434']

5、写一个正则表达式,使其能同时识别下面所有的字符串:'bat', 'bit', 'but', 'hat', 'hit', 'hut‘

import re

s="'bat', 'bit', 'but', 'hat', 'hit', 'hut"

content=re.findall(r"..t",s)

print content

 

c:\Python27\Scripts>python task_test.py

['bat', 'bit', 'but', 'hat', 'hit', 'hut']

6、匹配所有合法的python标识符

#coding=utf-8

 

import re

s="awoeur awier !@# @#4_-asdf3$^&()+?>



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3