Regular expressions (regexps) are patterns which describe the
contents of a string. They're used for testing whether a string
contains a given pattern, or extracting the portions that match. They are
created with the /pat/ and
%r{pat} literals or the
Regexp.new constructor.

A regexp is usually delimited with forward slashes (/). For
example:

/hay/=~'haystack'#=> 0/y/.match('haystack') #=> #<MatchData "y">

If a string contains the pattern it is said to match. A literal
string matches itself.

Here 'haystack' does not contain the pattern 'needle', so
it doesn't match:

/needle/.match('haystack') #=> nil

Here 'haystack' contains the pattern 'hay', so it matches:

/hay/.match('haystack') #=> #<MatchData "hay">

Specifically, /st/ requires that the string contains the
letter s followed by the letter t, so it matches
haystack, also.

=~ is Ruby's basic pattern-matching operator. When one
operand is a regular expression and the other is a string then the regular
expression is used as a pattern to match against the string. (This
operator is equivalently defined by Regexp and
String so the order of String and Regexp do
not matter. Other classes may have different implementations of
=~.) If a match is found, the operator returns index of first
match in string, otherwise it returns nil.

The following are metacharacters(, ),
[, ], {, },
., ?, +, *. They have a
specific meaning when appearing in a pattern. To match them literally they
must be backslash-escaped. To match a backslash literally, backslash-escape
it: \\.

A character class is delimited with square brackets
([, ]) and lists characters that may appear at
that point in the match. /[ab]/ means a or
b, as opposed to /ab/ which means a followed
by b.

/W[aeiou]rd/.match("Word") #=> #<MatchData "Word">

Within a character class the hyphen (-) is a metacharacter
denoting an inclusive range of characters. [abcd] is
equivalent to [a-d]. A range can be followed by another range,
so [abcdwxyz] is equivalent to [a-dw-z]. The
order in which ranges or individual characters appear inside a character
class is irrelevant.

If the first character of a character class is a caret (^) the
class is inverted: it matches any character except those named.

/[^a-eg-z]/.match('f') #=> #<MatchData "f">

A character class may contain another character class. By itself this
isn't useful because [a-z[0-9]] describes the same set as
[a-z0-9]. However, character classes also support the
&& operator which performs set intersection on its
arguments. The two can be combined as follows:

/[a-w&&[^c-g]z]/# ([a-w] AND ([^c-g] OR z))

This is equivalent to:

/[abh-w]/

The following metacharacters also behave like character classes:

/./ - Any character except a newline.

/./m - Any character (the m modifier enables
multiline mode)

/\w/ - A word character ([a-zA-Z0-9_])

/\W/ - A non-word character ([^a-zA-Z0-9_]).
Please take a look at Bug
#4044 if using /\W/ with the /i modifier.

/\d/ - A digit character ([0-9])

/\D/ - A non-digit character ([^0-9])

/\h/ - A hexdigit character ([0-9a-fA-F])

/\H/ - A non-hexdigit character ([^0-9a-fA-F])

/\s/ - A whitespace character: /[ \t\r\n\f\v]/

/\S/ - A non-whitespace character: /[^
\t\r\n\f\v]/

POSIX bracket expressions are also similar to character classes.
They provide a portable alternative to the above, with the added benefit
that they encompass non-ASCII characters. For instance, /\d/
matches only the ASCII decimal digits (0-9); whereas
/[[:digit:]]/ matches any character in the Unicode Nd
category.

/[[:alnum:]]/ - Alphabetic and numeric character

/[[:alpha:]]/ - Alphabetic character

/[[:blank:]]/ - Space or tab

/[[:cntrl:]]/ - Control character

/[[:digit:]]/ - Digit

/[[:graph:]]/ - Non-blank character (excludes spaces, control
characters, and similar)

Repetition is greedy by default: as many occurrences as possible
are matched while still allowing the overall match to succeed. By contrast,
lazy matching makes the minimal amount of matches necessary for
overall success. A greedy metacharacter can be made lazy by following it
with ?.

Both patterns below match the string. The first uses a greedy quantifier so
'.+' matches '<a><b>'; the second uses a lazy
quantifier so '.+?' matches '<a>':

A quantifier followed by + matches possessively: once
it has matched it does not backtrack. They behave like greedy quantifiers,
but having matched they refuse to “give up” their match even if this
jeopardises the overall match.

Parentheses can be used for capturing. The text enclosed by the
n<sup>th</sup> group of parentheses can be
subsequently referred to with n. Within a pattern use the
backreference\n; outside of the pattern use
MatchData[n].

'at' is captured by the first group of parentheses, then referred
to later with \1:

The (?:…) construct provides grouping without
capturing. That is, it combines the terms it contains into an atomic whole
without creating a backreference. This benefits performance at the slight
expense of readability.

The first group of parentheses captures 'n' and the second
'ti'. The second group is referred to later with the backreference
\2:

Grouping can be made atomic with
(?>pat). This causes the
subexpression pat to be matched independently of the rest of the
expression such that what it matches becomes fixed for the remainder of the
match, unless the entire subexpression must be abandoned and subsequently
revisited. In this way pat is treated as a non-divisible whole.
Atomic grouping is typically used to optimise patterns so as to prevent the
regular expression engine from backtracking needlessly.

The " in the pattern below matches the first character of
the string, then .* matches Quote“. This causes the
overall match to fail, so the text matched by .* is
backtracked by one position, which leaves the final character of the string
available to match "

/".*"/.match('"Quote"') #=> #<MatchData "\"Quote\"">

If .* is grouped atomically, it refuses to backtrack
Quote“, even though this means that the overall match fails

The \g<name> syntax matches the
previous subexpression named name, which can be a group name or
number, again. This differs from backreferences in that it re-executes the
group rather than simply trying to re-match the same text.

This pattern matches a ( character and assigns it to the
paren group, tries to call that the paren
sub-expression again but fails, then matches a literal ):

As mentioned above, the x option enables free-spacing
mode. Literal white space inside the pattern is ignored, and the octothorpe
(#) character introduces a comment until the end of the line.
This allows the components of the pattern to be organized in a potentially
more readable fashion.

A contrived pattern to match a number with optional decimal places:

float_pat = /\A
[[:digit:]]+ # 1 or more digits before the decimal point
(\. # Decimal point
[[:digit:]]+ # 1 or more digits after the decimal point
)? # The decimal point and following digits are optional
\Z/xfloat_pat.match('3.14') #=> #<MatchData "3.14" 1:".14">

There are a number of strategies for matching whitespace:

Use a pattern such as \s or \p{Space}.

Use escaped whitespace such as \ , i.e. a space preceded by a
backslash.

Use a character class such as [ ].

Comments can be included in a non-x pattern with the
(?#comment) construct, where
comment is arbitrary text ignored by the regexp engine.

Regular expressions are assumed to use the source encoding. This can be
overridden with one of the following modifiers.

/pat/u - UTF-8

/pat/e - EUC-JP

/pat/s - Windows-31J

/pat/n - ASCII-8BIT

A regexp can be matched against a string when they either share an
encoding, or the regexp's encoding is US-ASCII and the
string's encoding is ASCII-compatible.

If a match between incompatible encodings is attempted an
Encoding::CompatibilityError exception is raised.

The Regexp#fixed_encoding? predicate indicates whether the
regexp has a fixed encoding, that is one incompatible with ASCII.
A regexp's encoding can be explicitly fixed by supplying
Regexp::FIXEDENCODING as the second argument of
Regexp.new:

m = /s(\w{2}).*(c)/.match('haystack') #=> #<MatchData "stac" 1:"ta" 2:"c">$~#=> #<MatchData "stac" 1:"ta" 2:"c">Regexp.last_match#=> #<MatchData "stac" 1:"ta" 2:"c">$&#=> "stac"# same as m[0]$`#=> "hay"# same as m.pre_match$'#=> "k"# same as m.post_match$1#=> "ta"# same as m[1]$2#=> "c"# same as m[2]$3#=> nil# no third group in pattern$+#=> "c"# same as m[-1]

Certain pathological combinations of constructs can lead to abysmally bad
performance.

Consider a string of 25 as, a d, 4 as, and a
c.

s = 'a'*25+'d'+'a'*4+'c'#=> "aaaaaaaaaaaaaaaaaaaaaaaaadaaaac"

The following patterns match instantly as you would expect:

/(b|a)/=~s#=> 0/(b|a+)/=~s#=> 0/(b|a+)*/=~s#=> 0

However, the following pattern takes appreciably longer:

/(b|a+)*c/=~s#=> 26

This happens because an atom in the regexp is quantified by both an
immediate + and an enclosing * with nothing to
differentiate which is in control of any particular character. The
nondeterminism that results produces super-linear performance. (Consult
Mastering Regular Expressions (3rd ed.), pp 222, by Jeffery
Friedl, for an in-depth analysis). This particular case can be fixed
by use of atomic grouping, which prevents the unnecessary backtracking:

A similar case is typified by the following example, which takes
approximately 60 seconds to execute for me:

Match a string of 29 as against a pattern of 29 optional
as followed by 29 mandatory as:

Regexp.new('a?'*29+'a'*29) =~'a'*29

The 29 optional as match the string, but this prevents the 29
mandatory as that follow from matching. Ruby must then backtrack
repeatedly so as to satisfy as many of the optional matches as it can while
still matching the mandatory 29. It is plain to us that none of the
optional matches can succeed, but this fact unfortunately eludes Ruby.

The best way to improve performance is to significantly reduce the amount
of backtracking needed. For this case, instead of individually matching 29
optional as, a range of optional as can be matched all at
once with a{0,29}: