decoding:utf-8
ex_re_locatedSymbol.py
self_file = file #save current file absolute path
import re #使用正则表达式模块
#########4个定位符^、$、\b,\B##########
print u"\n^匹配一行文本开始处的文本"
"#请在正则表达式的开始使用^字符匹配一行文本开始处的文本"
lines = ["Hello world.", "hello world.", "ni hao", "Hello Tom"]
results = []
for line in lines:
if re.findall(r"^H", line): #找行首字符是H的文本行
results.append(line)
print results #['Hello world.', 'Hello Tom']
print u"\n$匹配一行文本结束处的文本"
"请在正则表达式的结束处使用 字符匹配一行文本的结束处的文本,"
lines = ["Hello world.", "hello world.", "ni hao", "Hello Tom"]
results = []
for line in lines:
if re.findall(r"m$", line):
results.append(line)
print results #['Hello Tom']
print u"\n\b匹配一个单词(不含空格的字符串)边界,即字与空格间的位置"
"请在正则表达式的开始处使用\b字符匹配单词(不含空格的字符串)开始处的文本"
"请在正则表达式的结束处使用\b字符匹配单词(不含空格的字符串)结束处的文本"
text = "apple took itake tattle tabled tax yed temperate"
print re.findall(r"\bta.\b", text) #ta开头的最长子句子 ['tattle tabled tax yed temperate']
print re.findall(r"\bta\S?\b", text) #ta开头的单词 ['tattle', 'tabled', 'tax']
print re.findall(r"\bta\S*?ed\b", text) #ta开头ed结尾的单词 ['tabled']
print u"\n\B表示非字边界匹配,对字边界也不匹配,\B不care在字中匹配的位置"
text = "phone phoneplus iphone telephone telegram"
从text中找出iphone telephone单词
words = text.split()
results = []
for word in words:
if re.findall(r"\Bphone", word):
results.append(word)
print results #['iphone', 'telephone']
print "\nexit %s" % self_file