fnmatch模塊: 提供對Unix Shell通配符的支持
Pattern Meaning
* matches everything
? matches any single character
[seq] matches any character in seq
[!seq] matches any character not in seq
'''
import os
import fnmatch
for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.py'):
print file
'''
glob模塊: 查找所有滿足Unix Shell模式規則的路徑名
'''
import os
import glob
for f in glob.glob(os.path.join(os.path.abspath('.'), '*')):
print f
Python的正則表達式類似於Perl語言。
re正則表達式使用'\'進行轉義, 而Python語言也使用'\'在字符串的轉義;因此,爲了匹配'\', 必須使用'\\\\'作爲模式。
因爲正則表達式必須是\\,而在字符串中每一個\需要兩個\\表示。
對於正則表達式模式,我們可以使用原始字符串(raw string)。原始字符串中的\只作爲普通字符串處理。因此,r'\n'表示兩個
字符,'\'和'n',但是在普通字符串中, '\n'將作爲換行符處理。
re中的特殊字符:
'.' 匹配任意字符, 除了換行。如果 DOTALL標記打開,將匹配所有字符,包括換行。
'^' 匹配字符串的開始。
'$' 匹配字符串的結束。
'*' 0或多次重複匹配。
'+' 1或多次重複匹配。
'?' 0次或1次匹配。
*?, +?, ?? 對應於'*', '+', '?'的非貪心匹配。
{m} m次重複匹配
{m, n} m到n次重複匹配,省略m表示m = 0, 省略n表示n無窮大。
{m, n}? 與*?, +?, ??類似, 非貪心匹配。
[] 匹配字符集。
| A|B,表示匹配A或B。
() 正則表達中組的概念。
\d 匹配十進制數
\D 匹配非非數字字符
\s 匹配空白
\S 匹配非空白
\w 匹配任意數字和字母
\W 匹配任意非數字和字母
url = 'http://www.contoso.com:8080/letters/readme.html'
obj = re.match(r'(.*)//(.*):(\d+)(.*)', url)
print obj.groups()
lstStr = ['local 127.0.0.1', 'Lucy 192.168.130.2', 'Link 192.168.130.224']
for s in lstStr:
obj = re.match(r'.*?(\d+.\d+.\d+.\d+).*?', s)
print obj.groups()