常用的匹配模式:

 正則表達式是根據一定的規則把字符串匹配處理

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 >逗你玩呢</a><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 >逗你玩呢</a><a )
# print(res)

# res=re.search('(href)="(.*?)"','<p>動感視頻</p><a >逗你玩呢</a><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))
找數字