Pacific-Design.com

    
Home Index

1. Python

2. Regex

+ Compile

Python / Regex /

Python - Regular Expression (regex)

#!/usr/bin/python3
import re

def main():
    fh = open('links.dat')
    for line in fh:
        if re.search('(google|yahoo)', line):
            print(line, end='')

if __name__ == "__main__": main()

#!/usr/bin/python3
import re

def main():
    fh = open('links.dat')
    for line in fh:
        print( re.sub('https', 'http', line), end='' )

if __name__ == "__main__": main()

#!/usr/bin/python3
import re

def main():
    fh = open('links.dat')
    for line in fh:
        match = re.search('games', line)
        if match:
            print(line.replace(match.group(), 'videogames'), end='')

if __name__ == "__main__": main()