CS 330 Lecture 8 – Regex Lookaround
Agenda
- what ?s
- regex bingo
- lookaround
- quoting HTML attributes
- humanizing identifiers
TODO
Right now:
- You and a partner make a 4×4 grid of randomly generated strings. Include upper- and lowercase letters, numbers, whitespace, and punctuation. Keep the strings short. There’s no free space.
Before Friday:
- Familiarize yourself with grammars and ANTLR. Read Grammar: The Language of Languages. On a 1/4 sheet, write a little grammar to match boolean expressions comprised of boolean literals, variable identifiers, binary operators
||
and&&
, and unary operator!
. Allow for parentheses to elevate the enclosed operation’s precedence:!(true || isTall)
.
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 `'