Skip to content

Instantly share code, notes, and snippets.

@rthbound
Forked from baweaver/html_flip_flop.rb
Created September 3, 2015 21:18
Show Gist options
  • Save rthbound/9ebdacdfc7cfdaac7ca9 to your computer and use it in GitHub Desktop.
Save rthbound/9ebdacdfc7cfdaac7ca9 to your computer and use it in GitHub Desktop.
Abusing flip flops for HTML parsing after watching a ruby tapa ala Avdi.
IO.readlines('test.html')
.flat_map(&:split)
.select { |word|
true if word =~ %r{<strong>} .. word =~ %r{</strong>}
}
# => ["<strong>multiple</strong>","<strong>strong</strong>"]
# test.html
#
# <html>
# <body>
# <h1>Foobar</h1>
#
# <p>Let's see how this works</p>
#
# <p>With <strong>multiple <strong>really strong</strong></strong> seperated <strong>strong</strong> words</p>
# </body>
# </html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment