re = /\\([a-zA-Z]+ *|.|\n)/
str = '\\documentclass {book}
\\usepackage{a}
\\begin {document}
\\chapter*{This is the title of a index-absent chapter}
\\I\'ll try to do some actual \\LaTeX\\ text to se\\footnote {This is text in a footnote.} how it will be matched.
\\chapter{This is {\\bf another} chapter title}
You have to be carefull with accents, like ``Espa\\~nol\'\'
and active characters (characters like the quotes above
or the newline character, which act by themselves as a
command).\\
You have to be carefull also with comments (beginning with
the percentage char) % this is one comment.
And of course you\'ll loose al your math, as in
$e^{i\\pi} + 1 = 0$.
\\end{document}
'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html