%%bash
time perl src/perl/slow.pl
%load src/perl/slow.pl
%%bash
time perl src/perl/fast.pl
%load src/perl/slow.pl;
%%bash
time perl src/perl/anchored.pl
%%bash
time perl src/perl/unanchored.pl
%%bash
time perl src/perl/greedy.pl
%%bash
time perl src/perl/nongreedy.pl
%%bash
time perl src/perl/good_alt.pl
%%bash
time perl src/perl/bad_alt.pl
(?:pattern)
%%bash
time perl src/perl/capture.pl
%%bash
time perl src/perl/noncapture.pl
re
module\
character in Python so the regex can process use a raw stringr'This is a raw python string\n'
import re
re
module has 4 methods to performing matchingif re.match(r'needle',r'Is there a needle in this haystack?'):
print "match"
if re.search(r'needle',r'Is there a needle in this haystack?'):
print "match"
true
or false
in PythonMatchObject
is returnedNone
is returnedMatchObject
can be used to access groups found in the match, as well as information such as positionre.search(r'needle',r'Is there a needle in this haystack?')
match = re.search(r'(\w+)\sneedle',r'Is there a needle in this haystack?')
print match.group(0)
print match.group(1)
g
modifier, Python has two specialized functionsMatchObjects
re.findall(r'\b\w*a\w*\b', r'Is there a needle in this haystack?')
re.findall(r'\b(\w*)a(\w*)\b', r'Is there a needle in this haystack?')
re.findall(r'(\w)\1', r'Is there a needle in this haystack?')
re.findall(r'(?P<a_letter>\w)(?P=a_letter)', r'Is there a needle in this haystack?')
re.sub
methodre.sub(pattern,replacement,string,count=0,flags=0)
re.sub
is global by default. To do only one substitution set the count
parameter to 1replacement
can be either a string or a function that takes a MatchObject
as it's argument\1
instead of $1
re.sub(r'(\w)\1','oo',r'Is there a needle in this haystack?')
re.sub(r'(\w)\1',r'\1',r'Is there a needle in this haystack?')
re
module can split strings using the split
methodre.split(regex,string,limit,flags)
re.split(r'[aeiou]+',r'Is there a needle in this haystack?')
re
modulere.search(r'n(\w)\1dle',r'Is there a\n NOODLE in this haystack')
match = re.search(r'n(\w)\1dle',r'Is there a\n NOODLE in this haystack',flags = re.I | re.M)
print match.start(), match.end(), match.pos, match.group(0), match.group(1)
re.compile(pattern,flags)
will return a RegexObject
RegexObject
are mostly the same as the re
module, but the pattern is no longer passsed as an argumentregex = re.compile(r'n(\w)\1dle',flags = re.I | re.M)
if regex.search('iS ThERe a \nNOoDLE iN This HaYStaCK'):
print "Match"
print regex.sub(r"z\1\1",'Is there a noodle in Baltimore?')
for match in regex.finditer('You shouldn\'t sew your clothes with a noodle, no matter how many needles you have'):
print match.group(0)