CS 330 Lecture 4 – Regular Expressions
January 29, 2014 by Chris Johnson. Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs330, lectures, spring 2014.
Agenda
- what ?s
- the gauntlet
- state machines/DFAs
- DFAs are regular expressions
- bingo
TODO
- Walk through http://regexone.com.
- Write expressions to match:
- Lines ending with a hyphenated word.
- Words with an internal uppercase letter.
- Lines lacking a semi-colon at their close. Don’t match lines that have a semi-colon followed by whitespace.
- Instances of the identifier
i. Don’t match other occurrences of i.
- 1/4 sheet.
While You’re Waiting
- We’re going to play some regex bingo later on. You and a partner make a 3×3 grid of randomly generated strings. Include upper- and lowercase letters, numbers, whitespace, and punctuation. Keep the strings short. There’s no free space.
State Machines


Code
show_matches.rb
#!/usr/bin/env ruby
txt = IO.read('eg.txt')
puts txt.scan(/[0-9]/)
puts '--------------'
puts txt.scan(/\S+@\S+/)
puts '--------------'
puts txt.scan(/\.\s+[a-z]/)
puts '--------------'
puts txt.scan(/<b>.*<\/b>/)
puts '--------------'
puts txt.scan(/<.*?>/)
eg.txt
asdf asdf 1 234 456
asdfkl sdf kdfklads f <b>56</b>
johnch@uwec.edu
asdfk foo@foo.com
The dog is <b>luscious</b>. The cat is blah. those who walk on rainbows peak at middle age.
Haiku
Can brains handle X?
Brains have handled X for years
They’re on to Y now