python regular expression basic

#!/usr/bin/python3
# regex.py by Bill Weinman [http://bw.org/]
# This is an exercise file from Python 3 Essential Training on lynda.com
# Copyright 2010 The BearHeart Gorup, LLC


import re


def main():
    fh = open('raven.txt')
    for line in fh:
        if re.search('(Len|Neverm)ore', line):
            print(line, end='')


if __name__ == "__main__": main()


#!/usr/bin/python3
# regex.py by Bill Weinman [http://bw.org/]
# This is an exercise file from Python 3 Essential Training on lynda.com
# Copyright 2010 The BearHeart Gorup, LLC


import re


def main():
    fh = open('raven.txt')
    for line in fh:
        print(re.sub('(Len|Neverm)ore','###', line),end=' ')


if __name__ == "__main__": main()


------預編譯

#!/usr/bin/python3
# regex.py by Bill Weinman [http://bw.org/]
# This is an exercise file from Python 3 Essential Training on lynda.com
# Copyright 2010 The BearHeart Gorup, LLC


import re


def main():
    fh = open('raven.txt')
    pattern=re.compile('(Len|Neverm)ore')
    for line in fh:
        if re.search(pattern, line):
            print(pattern.sub('###',line), end='')


if __name__ == "__main__": main()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章