Outline for November 2, 2023
Reading:
§14
Due:
Homework 3, due November 9, 2023
Example: memos
Remember how slowly the recursive Fibonacci number program
rfib.py
ran? Here is a faster recursive version that uses memos [
rfibmemo.py
]
Recursion: permutations of characters in a string [
perm.py
]
Pattern matching
Regular expressions
Atoms: letters, digits
Match any character except newline:
.
Match any of a set of characters:
[0123456789]
,
[^0123456789]
,
[0-9]
Repetition:
*
,
+
,
{
m
,
n
}; greedy matching; put
?
after and they match as few characters as possible
Match start, end of string:
^
,
$
;
$
matches end of line, also
Grouping:
(
,
)
Escape metacharacters:
\
Useful functions/methods [
recomp.py
,
renocomp.py
,
regroup.py
]
re.compile(
str
)
compiles the pattern into
pc
(that is,
pc = re.compile(str)
)
pc
.match(
str
)
returns None if compiled pattern
pc
does not match beginning of string
str
pc
.search(
str
)
returns None if pattern
pc
does not match any part of string
str
pc
.findall(
str
)
returns a list of substrings of the string
str
that match the pattern
pc
pc
.group(
str
)
returns the substring of the string
str
that the pattern
pc
matches
pc
.start(
str
)
returns the starting position of the match
pc
.end(
str
)
returns the ending position of the match
pc
.span(
str
)
returns tuple (start, end) positions of match
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email:
[email protected]
ECS 235A, Computer and Information Security
Version of November 1, 2023 at 10:02PM
You can also obtain a PDF version of this.