Regex quick reference (hover/tap for details)
-
[abc]
a|b|c[abc]
matches one of a, b, or c.Example: “cab” → matches “c”, “a”, or “b”.
-
[^abc]
not a|b|c[^abc]
matches any char except a, b, or c.Example: “dog” → “d”, “o”, “g” match; “a” doesn’t.
-
[a-z]
a..z[a-z]
matches a lowercase letter.Example: “ruby” → every letter matches.
-
[a-zA-Z]
letter[a-zA-Z]
matches any letter.Example: “v2” → matches “v”, not “2”.
-
^
start^
is start of line.Example:
^Hi
only at the beginning. -
$
end$
is end of line.Example:
done$
only at the end. -
\A
str start\A
is start of string (not just line).Example:
\AHi
matches only once at very start. -
\z
str end\z
is end of string.Example:
ok\z
matches only at very end. -
.
any.
matches any char (except newline unlessm
).Example: “cat” → “.” can be “c”, “a”, or “t”.
-
\s
space\s
whitespace: space, tab, newline.Example: “a b” → matches the space.
-
\S
non‑space\S
any non‑whitespace.Example: “ a” → matches “a”, not the space.
-
\R
line break\R
any linebreak.Example: “\n” → matches newline.
-
\d
digit\d
digit 0–9.Example: “v2” → matches “2”.
-
\D
non‑digit\D
any non‑digit.Example: “v2” → matches “v”, not “2”.
-
\w
word\w
letter/number/underscore.Example: “a_b” → matches “a”, “_”, “b”.
-
\W
non‑word\W
not letter/number/_.Example: “a-b” → matches “-”.
-
\b
boundary\b
word boundary.Example:
\bcat\b
finds “cat” as a whole word. -
(...)
capture(...)
capture group.Example:
(ab)+
captures repeats of “ab”. -
(a|b)
eithera|b
alternation: a or b.Example:
(cat|dog)
. -
a?
0 or 1 timesa?
zero or one “a”.Example:
colou?r
matches “color”/“colour”. -
a*
0+ timesa*
zero or more “a”.Example: “ba*” → “b”, “ba”, “baaa”.
-
a+
1+ timesa+
one or more “a”.Example: “ba+” → “ba”, “baaa” (not “b”).
-
a{3}
3× timesa{3}
exactly three “a”.Example: matches “aaa”.
-
a{3,}
3+ timesa{3,}
three or more.Example: “aaa”, “aaaa”, …
-
a{3,6}
3–6 timesa{3,6}
between three and six.Example: matches “aaa”..“aaaaaa”.
Options (hover/tap for details)
-
i
ignore casei
makes matches case‑insensitive.Example:
/cat/i
matches “Cat” and “cat”. -
m
dot = newlinem
makes.
match newline too.Example:
/a.b/m
can span lines. -
x
free‑spacingx
ignores spaces and# comments
in regex.Example:
/( \d+ ) # number/x
-
o
#{ } onceo
interpolates#{...}
only once.Example:
/#{Time.now}/o
keeps the first value.