python regular expression basic

来源:互联网 发布:coc狂暴升级数据 编辑:程序博客网 时间:2024/05/16 02:06
#!/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()

0 0
原创粉丝点击