Regular Expression in Ruby
In this post, I intend to write down some basic Regex knowledge, especially for Ruby. Syntax A regular expression literal is a pattern between slashes or between arbitrary delimiters followed by %r as follows: /pattern/ /pattern/im # option can be specified %r!/usr/local! # general delimited regular expression Basic patterns Except for control characters, (+ ? . * ^ $ ( ) [ ] { } | \ ), all characters match themselves. You can escape a control character by preceding it with a backslash. Following table lists the regular expression syntax that is available in Ruby. Patterns Description ^ Matches beginning of line. $ Matches end of line. . Matches any single character except newline. Using m option to allow it to match newline as well. […] Matches any single character in brackets. [^…] Matches any single character not in brackets re* Matches 0 or more occurrences of preceding expression. re+ Matches 1 or more occurrence of preceding expression. re? ...