Lecture 22: Florida Man and Regex
Overview
Regular expressions turn text-pattern searches and redaction into compact, reusable operations, illustrated by the risk of publishing sensitive information in Florida government emails. The lecture explains character classes, built-in shorthand, quantifiers, and Python's `re.sub`, then demonstrates how an eight-line file workflow can replace Social Security numbers with a redaction label; it also reviews the course's exam format.
Key takeaways
- The pattern `\d{3}-\d{2}-\d{4}` compactly matches a U.S. Social Security number's three-two-four digit layout, including the hyphens.
- In Python, `re.sub(pattern, replacement, text)` replaces every match, and writing the pattern as a raw string avoids double-escaping backslashes.
- Character classes and built-ins such as `[aeiou]`, `\d`, `\w`, and `\s` express common text-matching rules without manually listing every character.
- Quantifiers make regex concise: `?` means optional, `+` means one or more, `*` means zero or more, and `{n}` specifies an exact repetition count.
- A safe redaction workflow keeps sensitive records local, reads the source text, substitutes matches, and writes the result to a separate output file.
- Temple's final exam has seven 20-point questions, of which students choose five; regex is one of the available question topics alongside programming tasks such as file reading and Monte Carlo simulation.
Chapters
- Professor Rosen identifies the final exam slot by checking Temple University's published exam schedule.
- Temple final exams run from December 9 through December 15, with December 8 designated as a study day and no finals scheduled during the last week of classes.
- Students with more than two exams in one day should request an accommodation from an instructor and contact the dean if no arrangement is made.
- The final has seven questions, each worth 20 points, and students choose five for a 100-point exam.
- Question topics include list-of-lists processing, file reading, basic data analysis, Turtle drawings with 60-degree turns, Monte Carlo simulation, a conditional number-printing task, regex, and a documentation-based mystery problem.
- Students may attempt more than five but must specify which five to grade; otherwise, the first five are graded. A cheat sheet is allowed.
- The mystery problem provides focused documentation for an unfamiliar tool, and Professor Rosen says most students who attempt it earn at least 16 of 20 points.
- Exam three is planned for the final possible class day and is shorter than the two-hour final.
- Most exam-three material will cover regular expressions, including a question similar to the final's regex problem.
- The exam also revisits difficult areas from exam two, and the instructor plans to grade promptly so students can consider the approaching drop deadline.
- The Florida Man meme turns unusual Florida news headlines into a joke about one supposedly prolific person.
- Examples include a man riding a human-sized hamster wheel in the Atlantic Ocean and a man finishing a beer before being arrested.
- Florida's distinct identity, extreme weather, and hurricane-related insurance problems provide context for the state's reputation.
- Florida's broad public-records access makes government material, including police body-camera footage, relatively easy for journalists and the public to obtain.
- The lecture compares state rules such as Pennsylvania's two-party consent requirement for recording, while cautioning that recording laws vary by state.
- High public-records visibility and self-perpetuating memes help explain why Florida appears so often in unusual news stories.
- Redacting Social Security numbers requires finding a pattern such as three digits, a hyphen, two digits, another hyphen, and four digits.
- Uploading sensitive records to a cloud AI service can create privacy risks, so the lecture motivates an on-site text-processing approach.
- Regular expressions, or regex, describe text patterns compactly and are supported by programming languages and tools such as VS Code and Excel.
- Regexr provides an expression field, sample text, a cheat sheet, and explanations of matches.
- A literal expression such as `E` finds uppercase E characters because searches are case-sensitive by default.
- The question mark makes the preceding character optional, allowing one pattern to match both `flavor` and `flavour`.
- Square brackets define a character class, so `[aeiou]` matches one lowercase vowel at a time.
- A caret at the start of a character class negates it, so `[^aeiou]` matches characters that are not vowels, including spaces and punctuation.
- Ranges such as `[a-z]` match lowercase letters without listing each character, and a case-insensitive flag can include uppercase letters.
- `\d` matches digits, `\w` matches word characters such as letters, digits, and underscores, and `\s` matches whitespace such as spaces and tabs.
- Uppercase forms such as `\D` match the inverse set, in this case any non-digit.
- A period matches any character; use an escaped period, `\.`, to search for a literal dot.
- Curly-brace quantifiers specify repetition counts or ranges, such as `{3}`, `{1,5}`, or `{1,}`.
- The plus sign means one or more repetitions, while the star means zero or more repetitions of the preceding character or group.
- Parentheses group multi-character patterns, allowing `(ha)+` to match repeated `ha` units rather than just repeated instances of a single character.
- Combining these tools gives the Social Security number pattern `\d{3}-\d{2}-\d{4}`.
- Regex belongs to the theory of regular languages and corresponds in expressive power to finite-state machines, which can be understood as flowcharts with no memory.
- The Kleene star is named for mathematician Stephen Kleene and represents zero or more repetitions.
- Exam applications include identifying and redacting phone numbers, Temple email addresses, Social Security numbers, healthcare IDs, and shipping details.
- Python's `re` module supports substitutions through `re.sub(pattern, replacement, text)`.
- The example opens `target.txt` and uses `.read()` to load the entire file into one string rather than processing it line by line.
- A raw string such as `r"\d{3}-\d{2}-\d{4}"` preserves regex backslashes without requiring them to be escaped for Python.
- Replacing matches with `"redacted"` produces a new string that can be written to a separate output file.
- The example input file contains a deliberately fake Social Security number in the format `111-22-4321`.
- Running the script with Python creates `output.txt`, leaves `target.txt` unchanged, and replaces the matching number with `redacted`.
- Opening the output file verifies that the pattern and substitution work on a complete text file.
- The file-opening and writing steps remain largely the same across redaction tasks; the regex pattern is the part that changes.
- Regex avoids cumbersome chains of conditionals for checking each digit, hyphen, or character in a target string.
- The lecture closes by emphasizing that substitution is the regex operation most likely to appear on the exams.
Transcript excerpt
0:00 All right. So, first questions first. Um, exam three and also as a coral corollary the final exam. All right. Uh, typically all right. So, if I recall correctly, our last day of the semester is December 7th on Monday, right? That's just the last day, right? That's when your final project is due. Typically what I do because of time constraints and just constraints and okay and just because of constraints in general what I typically do is I hold exam three in the lecture on Thursday sorry on the day on like the last possible day of class. Okay everybody everybody get that?
0:51 Um, if you are happy with your scores for exam one and two, you can just take a zero on this exam and it gets dropped, right? Because of those three midterm grades, your you we drop the lowest, right? The final exam is a different beat all together. Um, the fi first off final exam. So, this is how I find out where when the final when the final exam is. Final exam. I look for final exam Temple University. And it will bring me to this PDF. Right here's the exam schedule says they're from December 9th to December 15th. And we're actually going to do the thing that we that you don't do because uh nobody does this. We're actually going to try reading this doc a
1:38 bit of this document because it contains some important information not just for this class but for all your classes. There's one study day from t on Tuesday 128. File exams may not be scheduled on study days or the last week of classes. That's right. Your professor can't try to get out of out of uh the university early by putting the exam on this last
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Rosen.