常用的匹配模式:

 正则表达式是根据一定的规则把字符串匹配处理

import re

# msg='1-2*(60+((-40.35)/5)-(-4*3))'
# print(re.findall('\D?(-?\d+\.?\d*)',msg))
print(re.findall('skye','skye is the best skye best'))
#找到字符串中所有'skye'的字符串依次放入一个列表
print(re.findall('\w','A)sd123s44_=0……'))
#找到字符串中所有字母数字和下划线
print(re.findall('\w\w','Asd123s44_=0……'))
#从头开始查找,若第一个满足\w的条件,则匹配第二个,若也满足条件,则放入列表。
#如果第二个字符不满足条件,则继续查找
# ^: 仅从头开始匹配,只限一行
# print(re.findall('^alex',' alex is alex is alex'))
#

# $: 仅从尾部开始匹配,只限一行

# print(re.findall('alex$',' alex is alex is alex1'))
# .: 代表匹配一个字符,该字符可以是除换行符之外任意字符
print(re.findall('a.c','a a1c aaac a c asfdsaf a\nc',re.DOTALL))

# ['a1c','aac','a c','a\nc']
# []:代表匹配一个字符,这一个字符是来自于我们自定义的范围
# print(re.findall('a[0-9]c','a,c a a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
# print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
# print(re.findall('a[a-zA-Z]c','a,c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
print(re.findall('a[+*/-]c','a,c a+c a-c a*c a/c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
print(re.findall('a[+*\-/]c','a,c a+c a-c a*c a/c aAc a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
print(re.findall('a[^0-9]c','a,c a a1c a9c aaac a c asfdsaf a\nc',re.DOTALL))
#在[]中用^表示除了这种情况
# 重复匹配
# ?:代表左边那一个字符出现0次到1次
print(re.findall('a\b?','a ab abb abbbb a123b a1b23bbbb'))
#                                            ab?
# ['a','ab','ab','ab','a','a']
# .*: 匹配任意0个到无穷个字符,贪婪匹配
# print(re.findall('a.*c','a123213123asdfasdfc123123123123+-0)((c123123'))
#                        a.*c

#.*?:匹配任意0个到无穷个字符,非贪婪匹配
# print(re.findall('a.*?c','a123213123asdfasdfc123123123123+-0)((c123123'))
#|:或者
# print(re.findall('companies|company','Too many companies have gone bankrupt,c and the next one is my company'))
#                                                                                                    companies|company

#():分组
# print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt,c and the next one is my company'))
#                                                                                                compan(ies|y)

# print(re.findall('href="(.*?)"','<p>动感视频</p><a href="https://www.douniwan.com/1.mp4">逗你玩呢</a><a href="https://www.xxx.com/2.mp4">葫芦娃</a>'))
#                                              href=".*?"
#忽略大小写
# print(re.findall('alex','my name is alex Alex is dsb aLex ALeX',re.I))

# msg="""
# my name is egon
# asdfsadfadfsadf egon
# 123123123123123egon
# """
# print(re.findall('egon$',msg,re.M)) #my name is egon\nasdfsadfadfsadf egon\n123123123123123egon'

#re模块其他方法
# res=re.findall('(href)="(.*?)"','<p>动感视频</p><a href="https://www.douniwan.com/1.mp4">逗你玩呢</a><a href="https://www.xxx.com/2.mp4">葫芦娃</a>')
# print(res)

# res=re.search('(href)="(.*?)"','<p>动感视频</p><a href="https://www.douniwan.com/1.mp4">逗你玩呢</a><a href="https://www.xxx.com/2.mp4">葫芦娃</a>')
# print(res)
# print(res.group(0))
# print(res.group(1))
# print(res.group(2))


# res=re.match('abc','123abc') ## res=re.search('^abc','123abc')
# print(res)

# print(re.findall('alex','alex is alex is alex'))
# print(re.search('alex','alex is alex is alex'))
# print(re.match('alex','alex is alex is alex'))

# pattern=re.compile('alex')
# print(pattern.findall('alex is alex is alex'))
# print(pattern.search('alex is alex is alex'))
# print(pattern.match('alex is alex is alex'))
View Code

 

['1', '2', '60', '-40.35', '5', '-4', '3']
msg="1-2*(60+(-40.35/5)-(-40*3))"
print(re.findall('\D?(-?\d+\.?\d*)',msg))
找数字