import re def find(pat, text): match = re.search(pat, text) # left to right, find first match then return, or return none if match: print match.group() else: print "not found." # add a 'r' in frount of a text, tells python not change this string, just pass it to the function.(usefull when the string have '\') find(r'a.b', "asd aab aaaab") # \w matchs [0-9][a-z][A-Z], one charector find(r'\w\w\w', "a ab ab~ abc") # \d matchs number find(r'\d\d\d', "1, 12 12, 123") # \s matchs space/tab find(r'\d\s\d', "1 a 2 3; 1 1") # \S matchs not space/tab find(r'\S+', " sdasdasd!@#$@!dsfds * 11") # +:1 or more; *: 0 or more find(r'\w+', "!@#$# this123123") # []: match anything in the [] (in it, the '.' do not need '\') # '.' matchs anything except \n???(one line) use DOTALL to change it find(r'\w[\w.]*@\w[\w.]+', "asdf .@aa.com a@a.com .ffff@bb.com sdf@@ @") # (): mark groups m = re.search(r'(\w[\w.]*)@(\w[\w.]+)', "asdf .@aa.com .ffff@bb.com sdf@@ @") print 'group=', m.group(), 'group(0)=', m.group(0) print 'group(1)=', m.group(1), 'group(2)=', m.group(2) # find all matchs list = re.findall('\w+\s\w+', "this is a big word"); print list # find all matchs as a tuple list list = re.findall('(\w+)\s(\w+)', "this is a big word"); print list ################################## funny syntax ############################################### a = [1, 2, 3, 4, 5, 6, 7] b = [w*w for w in a if w > 3] print b ################################################################################################ #!/usr/bin/python # Copyright 2010 Google Inc. # Licensed under the Apache License, Version 2.0 # http://www.apache.org/licenses/LICENSE-2.0 # Google's Python Class # http://code.google.com/edu/languages/google-python-class/ """ import sys import re import os import glob """ """Baby Names exercise Define the extract_names() function below and change main() to call it. For writing regex, it's nice to include a copy of the target text for inspiration. Here's what the html looks like in the baby.html files: ... <h3 align="center">Popularity in 1990</h3> .... <tr align="right"><td>1</td><td>Michael</td><td>Jessica</td> <tr align="right"><td>2</td><td>Christopher</td><td>Ashley</td> <tr align="right"><td>3</td><td>Matthew</td><td>Brittany</td> ... Suggested milestones for incremental development: -Extract the year and print it -Extract the names and rank numbers and just print them -Get the names data into a dict and print it -Build the [year, 'name rank', ... ] list and print it -Fix main() to use the extract_names list """ """ def extract_names(filename): # +++your code here+++ file = open(filename, 'r') text = file.read() yearMatch = re.search(r'Popularity in \d\d\d\d', text) nameTuples = re.findall(r'<tr align="right"><td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>', text) dic = {} for nameTuple in nameTuples: name = nameTuple[1] dic[name] = nameTuple[0] name = nameTuple[2] dic[name] = nameTuple[0] dic = sorted(dic.items()) if yearMatch: print(yearMatch.group()) file.close() return dic def main(): # This command-line parsing code is provided. # Make a list of command line arguments, omitting the [0] element # which is the script itself. args = sys.argv[1:] if not args: print('usage: [--summaryfile] file [file ...]') sys.exit(1) # Notice the summary flag and remove it from args if it is present. summary = False if args[0] == '--summaryfile': summary = True del args[0] # +++your code here+++ # For each filename, get the names, then either print the text output # or write it to a summary file for regFilename in args: filenames = glob.glob(regFilename) for filename in filenames: if os.path.isfile(filename): dic = extract_names(filename) if not summary: for (name, rank) in dic: print(name, rank) else: f = open(filename+'summary.txt', 'w') for (name, rank) in dic: f.write(name + ' ' + rank + '\n') f.close() if __name__ == '__main__': main() """
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question