2020.1.3正则小练习

indall()
返回所有包含匹配项的列表,若未找到匹配,则返回空列表
>>>
>>> import re
>>>
>>> #Return a list containing every occurrence of "ai":
...
>>> str = "China is a great country"
>>> x = re.findall("a", str)
>>> print(x)
['a', 'a', 'a']
>>> import re
>>>
>>> str = "China is a great country"
>>> x = re.findall("USA", str)
>>> print(x)
[]
>>>

 

search()
函数搜索字符串中的匹配项,如果存在匹配则返回Match对象;如果未找到匹配,则返回值为None
如果有多个匹配,则仅返回首个匹配项
import re
str = "China is a great country"
x=re.search("\s",str)
print(x.start())
执行结果:
>>> import re
>>> str = "China is a great country"
>>> x=re.search("\s",str)
>>> print(x.start())
5
>>>


split()函数
返回一个列表,其中字符串在每次匹配时被拆分
import re
str = "China is a great country"
x=re.split("\s",str)
print(x)
执行结果:
>>> print(x)
['China', 'is', 'a', 'great', 'country']
>>>

sub()函数:函数把匹配替换为您选择文本
import re
str = "China is a great country"
x=re.sub("\s","9",str)
print(x)
执行结果:
>>> print(x)
China9is9a9great9country

 

match对象
Match对象是包含有关搜索和结果信息的对象。
注释:如果没有匹配,则返回值为None,而不是Match对象
import re
str = "China is a great country"
x=re.search("a",str)
print(x)
执行结果:
>>> print(x)
<_sre.SRE_Match object; span=(4, 5), match='a'>
>>>


span()返回的元组包含了匹配的开始和结果
.string返回传入函数的字符串
group()返回匹配的字符串部分

实例
打印首个匹配出现的位置(开始和结束位置)。
正则表达式查找以大写 "C" 开头的任何单词:
import re
str = "China is a great country"
x=re.search(r"\bC\w+",str)
print(x.span())
执行结果:
>>> import re
>>> str = "China is a great country"
>>> x=re.search(r"\bC\w+",str)
>>> print(x.span())
(0, 5)
>>>

实例
打印传入函数的字符串:
import re
str = "China is a great country"
x=re.search(r"\bC\w+",str)
print(x.string)
执行结果:
>>> print(x.string)
China is a great country
>>>

 

实例
打印匹配的字符串部分。
正则表达式查找以大写 "C" 开头的任何单词:
import re
str = "China is a great country"
x=re.search(r"\bC\w+",str)
print(x)

 

10、使用正则提取出字符串中的单词
方法一
s = "you are a good boy! 12345"
import re
name_list=re.match(r"\b[a-zA-Z]+\b",s)
print(name_list)


11、使用正则表达式匹配合法的邮件地址
email = ["hhq123_a@qq.com","666@163.org"]
import re
pattern=re.compile(r"\w+@\w+.\w+")
for i in email:
print(pattern.match(i).group())

执行结果:
...
hhq123_a@qq.com
666@163.org
>>>


16、写一个函数,其中用正则验证密码的强度
#至少包含6个字符,至少一个字母、一个数字,只能包含字母数字
def check_password(password):
import re
if re.match(r"(?=.*[A-Za-z])(?=.*[0-9][a-zA-Z0-9]{6,})",password):
print("Strong password!")
else:
print("Weak password!")

check_password("ad3wddd4444")
check_password("4565vgggg")
check_password("111111111")
执行结果:
>>> check_password("ad3wddd4444")
Strong password!
>>> check_password("4565vgggg")
Strong password!
>>> check_password("111111111")
Weak password!
>>>

 


15、匹配ip的正则表达式:
import re
pattern
re.compile(r"[1-9]|[1-9]\d|1\d\d|2[0-4]"\d|25[0-5]).){3}([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]$")
pattern.match("192.168.1.19").group()

1 、关于python保留几位小数,不进行四舍五入的方法
方法一:
def cut(num,c):
c=10**(-c)
return (num//c)*c

print(cut(2.99,3))
...
>>> print(cut(2.99,3))
2.99

方法二:
a=0.234325444444
print(a)
s="%f"%a
s=s[:s.index(".")+5]
print(s)
执行结果:
>>> a=0.234325444444
>>> print(a)
0.234325444444
>>> s="%f"%a
>>> s=s[:s.index(".")+5]
>>> print(s)
0.2343
>>>

方法三:正则表达式
re.search(r"\d+\.\d{4}",str(0.0555555555556)).group()

>>> re.search(r"\d+\.\d{4}",str(0.0555555555556)).group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>>


问题

1 、关于python保留几位小数,不进行四舍五入的方法
用正则报错
re.search(r"\d+\.\d{4}",str(0.0555555555556)).group()

>>> re.search(r"\d+\.\d{4}",str(0.0555555555556)).group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>>


group()的用法
\d?*+的[]混合到底是怎么结合使用的?

posted @ 2020-01-03 00:38  进阶的淑琴  阅读(154)  评论(0)    收藏  举报