regular-expressions snippets

Text processing with Ruby regular expressions and blocks

Tagged ruby, regex, regular-expressions, text-processing  Languages ruby

This example will find all code tags and pass the lang attribute's value and the code block's content to the block:

output = r.gsub(/<code[\.|\:]([\w\-]+)>(.*?)<\/code[\.|\:]?\1?>/m) do
       lang, text = $1, $2
       html = "<notextile>" + text +  "</notextile>"

The block's return value will replace the found tag.