Copy & Paste Python code from https://developers.google.com/edu/python/regular-expressions
#Python Regular Expressions;
import re
match = re.search('iig', 'called piiig')
print match.group()
def Find(pat, text):
match = re.search(pat, text)
if match: print match.group()
else: print 'Not Found'
# Basic Examples
. indicate any char
\w word char
\d digit
\s whitespace
\S non-whitespace
+ 1 or more
* 0 or more
Find('iig', 'called piiig')
Find('...g', 'callled piiig')
Find('..gs', 'called piiig')
Find('x..g', 'callled piig much better :xyzgs')
Find(r'c\.l', 'c.llled piig much better :xyzgs')
Find(r':\w\w\w', 'blah :cat blah blah')
Find(r'\d\d\d', 'blah : 123****')
Find(r'\d\s\d\s\d', 'blah : 1 2 3 ****')
Find(r'\d\s+\d\s+\d', '1 2 3')
Find(r':\w+', 'blah blah :kitten blabh blah')
Find(r':\w+', 'blah blah :kitten123 blabh blah')
Find(r':\w+', 'blah blah :kitten& blabh blah')
Find(r':.+', 'blah blah :kitten blabh blah')
Find(r':\w+', 'blah blah :kitten123123 blabh blah')
Find(r':\S+', 'blah blah :kitten123123&a=123&yatta blabh blah')
# Email examples
Find(r'\w+@\w+', 'purple alice-b@google.com monkey dishwasher')
Find(r'\w+@\w+', 'purple alice-b@google.com monkey dishwasher')
Find(r'\w[\w.]*+@[\w.]+', 'purple alice-b@google.com monkey dishwasher')
# Group Extraction
m = re.search(r'([\w.]+)@([\w.]+)', 'purple alice-b@google.com monkey dishwasher ')
print m.group()
print m.group(1)
print m.group(2)
# findall With Files
m = re.findall(r'[\w.-]+@[\w.]+', 'purple alice-b@google.com monkey dishwasher foo@bar')
m = re.findall(r'([\w.-]+)@([\w.]+)', 'purple alice-b@google.com monkey dishwasher foo@bar')
print m
# findall and Groups
str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher'
tuples = re.findall(r'([\w\.-]+)@([\w\.-]+)', str)
print tuples ## [('alice', 'google.com'), ('bob', 'abc.com')]
for tuple in tuples:
print tuple[0] ## username
print tuple[1] ## host
# Utilities
# File System -- os, os.path, shutil
import os
dir(os)
help(os.listdir)
# Hello.py contain examples
# Running External Processes -- commands
print os.path.exists('/tmp/foo')
import shutil
#shutil.copy(source, dest)
import commands
#cmd = 'ls'
#print commands.getstatusoutput(cmd)
# Exceptions
# HTTP -- urllib and urlparse
import urllib
uf = urllib.urlopen('http://google.com')
uf.read()
urllib.urlretrieve("http://google.com/...*gif", 'blah.gif')
# cat Hello.py
import sys
import os
import commands
def list(dir):
# File System -- os, os.path, shutil
filenames = os.listdir(dir)
print filenames
for filename in filenames:
print filename
print os.path.join(dir, filename)
print os.path.abspath(os.path.join(dir, filename))
# Running External Processes -- commands
cmd = 'ls -l ' + dir
(status, output) = commands.getstatusoutput(cmd)
if status:
sys.stderr.write(output)
sys.exit(1)
print output
def Hello(name):
if name == 'Alice' or name == 'Nick':
name = name + '?????'
DoesNotExit
else:
name = name + "!!!!!"
print 'Hello', name
def Cat(filename):
try:
f = open(filename, 'rU')
print '-----', filename
#for line in f:
# print line,
#lines = f.readlines()
#print lines
text = f.read()
print text,
f.close()
except IOError:
sys.stderr.write('problem reading:' + filename)
def main():
print Cat(sys.argv[1])
#print Hello(sys.argv[1])
#print list(sys.argv[1])
if __name__=='__main__':
main()
print 'this is {1}, that is {0}'.format(a,b)
print 'this is {0}, that is {1}'.format(a,b)
print 'this is {0}, that is {1}, this is too is {1}'.format(a,b)
print 'tis is {bob} and that is {fred}'.format(bob = a, fred = b)
d = dict(bob = a, fred = b)
print 'this is {bob} and that is {fred}'.format(**d)
No comments:
Post a Comment