teaching machines

CS 330 Lecture 8 – Regex Lookaround

February 10, 2016 by . Filed under cs330, lectures, spring 2016.

Agenda

TODO

Right now:

Before Friday:

Note

Today we really do close out our discussion of regular expressions by looking at a couple of examples of lookaround assertions. These assertions essentially turn what-to-match expressions into where-to-match anchors that don’t count as part of the matching text.

But first, some regex bingo!

  1: \b[A-Z]
  2: \s\w\s
  3: ^\d
  4: ^\w{3}$
  5: \.\d
  6: \d\w+\d
  7: [$?!#]{2}
  8: ^$
  9: ^(\d).*\1$
 10: \s\d
 11: \(.*\)
 12: ^[^abc]
 13: \$\w+
 14: ^\d+$
 15: [02468][A-Z]
 16: (^\.|\.$)
 17: ^.{4}$
 18: (.)\1\1
 19: [a-m].?[n-z]
 20: [A-Z][a-z]
 21: l[ao]
 22: .\D{2,4}.
 23: ^[A-Z][a-z]*$
 24: \d[^A-Za-z0-9]

Then we jump into the world of grammars. Grammars help our tools decompose our source code into meaningful pieces and then translate them into another language or evaluate those pieces directly. Assignment two in this class will involve writing our own little language.

Code

bad.html

<!DOCTYPE html>
<html>
<head>
  <title>...</title>
</head>
<body>
  <img src="foo.jpg" width=512>
  <h1 class=banner disabled=surely>
</body>
</html>

fixbadhtml.rb

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'

humanidy.rb

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'