我有一個Python程序,根據正則表達式peattern搜索有效的電話號碼文件。然後,如果它發現匹配,則將該數字解析並將其打印在屏幕上。如果有擴展名,我想修改它以識別擴展名。我添加了第二種模式(patStringExten
),但我不確定如何使其解析出擴展名。任何幫助,將不勝感激!Python中的正則表達式搜索
import sys
import re
DEF_A_CODE = "None"
def usage() :
print "Usage:"
print "\t" + sys.argv[0] + " [<file>]"
def searchFile(fileName, pattern) :
fh = open(fileName, "r")
for l in fh :
l = l.strip()
# Here's the actual search
match = pattern.search(l)
if match :
nr = match.groups()
# Note, from the pattern, that 0 may be null, but 1 and 2 must exist
if not nr[0] :
aCode = DEF_A_CODE
else :
aCode = nr[0]
print "area code: " + aCode + \
", exchange: " + nr[1] + ", trunk: " + nr[2]+ ", extension: " + nr[3]
else :
print "NO MATCH: " + l
fh.close()
def main() :
# stick filename
if len(sys.argv) < 2 : # no file name
# assume telNrs.txt
fileName = "telNrs.txt"
else :
fileName = sys.argv[1]
# for legibility, Python supplies a 'verbose' pattern
# requires a special flag
#patString = '(\d{3})*[ .\-)]*(\d{3})[ .\-]*(\d{4})'
patString = r'''
# don't match beginning of string (takes care of 1-)
(\d{3})? # area code (3 digits) (optional)
[ .\-)]* # optional separator (any # of space, dash, or dot,
# or closing ')')
(\d{3}) # exchange, 3 digits
[ .\-]* # optional separator (any # of space, dash, or dot)
(\d{4}) # number, 4 digits
'''
patStringExten = r'''
# don't match beginning of string (takes care of 1-)
(\d{3})? # area code (3 digits) (optional)
[ .\-)]* # optional separator (any # of space, dash, or dot,
# or closing ')')
(\d{3}) # exchange, 3 digits
[ .\-]* # optional separator (any # of space, dash, or dot)
(\d{4}) # number, 4 digits
[ .\-x]*
[0-9]{1,4}
'''
# Here is what the pattern would look like as a regular pattern:
#patString = r'(\d{3})\D*(\d{3})\D*(\d{4})'
# Instead of creating a temporary object each time, we will compile this
# regexp once, and store this object
pattern = re.compile(patString, re.VERBOSE)
searchFile(fileName, pattern)
main()
你在這裏問什麼?如何用'patStringExten'而不是'patString'調用'searchFile'?如何調用兩次,每次調用一次?如何將兩者合併成接受任一版本的單一模式?如何將比賽分成可以通過姓名或號碼拉出的組? – abarnert
接受任一版本的模式是什麼?如果有擴展名,我將如何打印出擴展名? – Albert