Programming
Workshop

Spring 2012
course
navigation

Mar 9

We gave some examples of regular expressions.

Perl: $foo = q(This is some text. Jim's email is jim@there.com. z@z But there are other @ and @. things. Can you extract the first.last@place.foo.com things?); print $foo . "\n"; if (@answer = $foo =~ /[a-z0-9.]+@[a-z0-9.]+\.[a-z]+/gi) { print "yes : @answer \n"; } else { print "no\n"; }

Python: import re foo = """This is some text. Jim's email is jim@there.com. z@z But there are other @ and @. things. Can you extract the first.last@place.foo.com things?""" print foo answer = re.findall(r"[a-z0-9.]+@[a-z0-9.]+\.[a-z]+", foo, flags=re.IGNORECASE) if (answer): print "yes: " + str(answer) else: print "no"

printing primes with regex (by Abigail):
perl -e'while($.++<99){$_.=1;print$..$/if!/^1?$|^(11+?)\1+$/}'
Recognizing winning Tic Tac Toe positions with regex : http://www.perlmonks.org/?node_id=307918
http://cs.marlboro.edu/ courses/ spring2012/programming/ notes/ Mar_9
last modified Friday March 9 2012 2:38 pm EST