/regex rosetta stone/i

Ruby

Onigmo
About
Backtracking engine. Line anchors always work per line, and the m flag means dot matches newline.
Verified on
Ruby 4.0.9
Offsets are counted in code points.
Found in
ruby
its own documentation
Flags
i ignore casem dotallx verbose
Character classes 13 of 26
SyntaxMatchesNotesTry
. Any characterAny single character. Whether that includes a newline depends on the dialect and its flags.

Does not match a newline.

test
[abc]not[^abc] One of a set of characters test
[a-z] One of a range of characters test
[\]] Backslash escape inside a setInside a bracket expression a backslash takes the special meaning from the next character. Where this is missing, a backslash there is a member like any other. test
[\d] Class escape inside a setA class escape such as the one for a digit, used as a member of a bracket expression. test
[\D] Negated class escape inside a setA negated class escape, such as the one for anything but a digit, used as a member of a bracket expression. test
\d[[:digit:]]not\D Digit

ASCII digits only: 0 to 9.

test
\w[[:alnum:]_]not\W Word characterA letter, a digit or an underscore.

ASCII only: A to Z, a to z, 0 to 9 and _.

test
\s[[:space:]]not\S Whitespace

Does not match the no-break space.

test
n/a Horizontal whitespaceA space or a tab, but not a line break. not in Ruby

Tried: \h compiles, and means something else.

who has it
n/a Vertical whitespaceA line break character of any kind. not in Ruby

Tried: \v compiles, and matches as plain text or not at all.

who has it
n/a Any character except newlineNever matches a newline, whatever the flags. not in Ruby

Tried: \N compiles, and matches as plain text or not at all.

who has it
\R Any line break sequenceOne line break, with CR LF taken as a single unit. test
n/a Alphabetic character (shorthand) not in Ruby

Tried: \a compiles, and matches as plain text or not at all.

who has it
n/a Lowercase letter (shorthand) not in Ruby

Tried: \l compiles, and matches as plain text or not at all.

who has it
n/a Uppercase letter (shorthand) not in Ruby

Tried: \u does not compile.

who has it
\hnot\H Hexadecimal digit (shorthand) test
[X&&Y] Class intersectionCharacters that are in both classes. test
[X&&[^Y]] Class subtractionCharacters in the first class that are not in the second. test
n/a Head-of-word character (shorthand)A letter or the underscore: what a word may start with. not in Ruby

Tried: \h compiles, and means something else.

who has it
n/a Octal digit (shorthand) not in Ruby

Tried: \o compiles, and matches as plain text or not at all.

who has it
n/a Identifier characterA character allowed in an identifier. In Vim the set comes from the 'isident' option. not in Ruby

Tried: \i compiles, and matches as plain text or not at all.

who has it
n/a Keyword characterA character allowed in a keyword. In Vim the set comes from the 'iskeyword' option. not in Ruby

Tried: \k compiles, and matches as plain text or not at all.

who has it
n/a File name characterA character allowed in a file name. In Vim the set comes from the 'isfname' option. not in Ruby

Tried: \f compiles, and matches as plain text or not at all.

who has it
n/a Printable characterA character that is displayed as itself. In Vim the set comes from the 'isprint' option. not in Ruby

Tried: \p compiles, and matches as plain text or not at all.

who has it
n/a Any single byteOne code unit, even where that is part of a longer character, and even a newline. not in Ruby

Tried: \C does not compile.

who has it
Anchors 9 of 14
SyntaxMatchesNotesTry
^ Start of the text or lineStart of the text. With a multiline flag, or always in some dialects, also the start of each line.

Matches at the start of every line, not only at the start of the text.

test
$ End of the text or line

Also matches before a newline that ends the text.

Matches at the end of every line, not only at the end of the text.

test
\A Start of the textStart of the text only, whatever the flags. test
\z End of the textThe very end of the text, whatever the flags. test
\Z End of the text, or before a final newline test
\b Word boundaryThe position between a word character and a non-word character or an end of the text.

Unicode-aware: a letter outside ASCII is a word character.

test
\B Not a word boundary test
n/a Start of a word not in Ruby

Tried: \<foo compiles, and matches as plain text or not at all.

who has it
n/a End of a word not in Ruby

Tried: foo\> compiles, and matches as plain text or not at all.

who has it
\G Where the previous match endedThe position where the last match stopped, or the start of the text for the first match. test
\K Reset the start of the matchEverything matched so far is required but left out of the reported match. test
n/a Start of symbolBefore a run of word and symbol characters, such as an identifier with an underscore in it. not in Ruby

Tried: \_<b compiles, and matches as plain text or not at all.

who has it
n/a End of symbol not in Ruby

Tried: a\_> compiles, and matches as plain text or not at all.

who has it
n/a Cursor positionWhere the editor's cursor is in the buffer being searched. not in Ruby

There is no buffer or cursor. The nearest anchor is \G: where the search begins when match is given an offset, and where the last match finished inside gsub and scan.

doc Not run on an engine: taken from the documentation.

who has it
Quantifiers 9 of 9
SyntaxMatchesNotesTry
X? Zero or one test
X* Zero or more test
X+ One or more test
X{N} Exactly N times test
X{N,} N or more times test
X{,M}X{0,M} At most M times test
X{N,M} Between N and M times test
X*?X+?X?? Lazy quantifierRepeat as few times as possible instead of as many as possible. test
X*+X++X?+ Possessive quantifierRepeat as many times as possible and never give any of it back. test
Groups and alternation 14 of 16
SyntaxMatchesNotesTry
(X) Capture group test
(?:X) Non-capturing group test
(?<NAME>X)(?'NAME'X) Named capture group

A pattern with a named group does not capture its unnamed groups.

test
X|Y Alternation

Takes the first alternative that matches, even when a later one is longer.

test
\N Backreference by numberThe same text that capture group N matched. partial

Not allowed in a pattern that has a named group: there a group is referred to by its name.

test
\k<NAME>\k'NAME' Backreference by name test
\k<-N> Backreference by relative positionThe group N groups back from here. partial

Not allowed in a pattern that has a named group: there a group is referred to by its name.

test
(?>X) Atomic groupOnce the group has matched, the engine never tries a different way to match it. test
n/a Branch reset groupEvery alternative numbers its capture groups from the same start. not in Ruby

Tried: (?|(a)|(b)) does not compile.

who has it
(?(N)X|Y) ConditionalMatch one pattern if a capture group took part, otherwise another. test
\g<0> Recurse into the whole pattern test
\g<N> Call a group as a subroutineMatch the pattern of capture group N again at this point, not the text it matched. partial

Not allowed in a pattern that has a named group: there a group is referred to by its name.

The call sets the group to what the call matched.

test
(?#TEXT) Comment test
n/a Explicitly numbered groupA capture group that is given its number, whatever its position. not in Ruby

Tried: (?<2>a) does not compile.

who has it
\g<-N> Call an earlier group by relative positionMatch the pattern of the Nth group before this point again. partial

Not allowed in a pattern that has a named group: there a group is referred to by its name.

The call sets the group to what the call matched.

test
\g<+N> Call a later group by relative positionMatch the pattern of the Nth group after this point here as well. partial

Not allowed in a pattern that has a named group: there a group is referred to by its name.

test
Lookaround 4 of 4
SyntaxMatchesNotesTry
(?=X) LookaheadWhat follows must match X, but is not part of the match. test
(?!X) Negative lookahead test
(?<=X) LookbehindWhat comes before must match X, but is not part of the match. test
(?<!X) Negative lookbehind test
Escape sequences 14 of 16
SyntaxMatchesNotesTry
\. Escape a special characterTake the next character literally. test
n/a Quote a run of textTake everything between the markers literally. not in Ruby

Tried: \Qa.b\E compiles, and matches as plain text or not at all.

who has it
\t Tab test
\n Newline test
\r Carriage return test
\f Form feed test
\v Vertical tab test
\a Alert (bell) test
[\b] Backspace test
\e Escape character test
\cC Control characterThe control character typed as Ctrl plus the letter. test
\OOO Character by octal code test
\xHH Character by two-digit hex code test
\u{HHHH} Character by Unicode code point test
n/a Character by Unicode name not in Ruby

Tried: \N{DIGIT ONE} compiles, and matches as plain text or not at all.

who has it
\x00\0\000\u0000 NUL characterThe character with code 0.

The octal escape reads one to three digits, so write \000 or \x00 before an octal digit. \0 means the whole match only in a replacement string, never inside the pattern.

doc Not run on an engine: taken from the documentation.

Flags and modes 10 of 14
SyntaxMatchesNotesTry
flag i(?i) Ignore case

A flag before | takes the alternatives after it into its scope: a(?i)b|c is read as a(?i:b|c).

test
^$ MultilineThe line anchors also match at the start and end of every line. partial

Always on, with no flag to turn it off: ^ and $ match at every line. Use \A and \z for the ends of the text. The m flag is not this mode: in Ruby it makes the dot match a newline.

test
flag m(?m) Dot matches newline

Ruby calls this flag m. There is no s flag.

(?s) is not accepted.

test
flag x(?x) Free spacingWhitespace in the pattern is ignored, and a # starts a comment. test
n/a Swap greedy and lazy not in Ruby

Tried: (?U)a.*c does not compile.

who has it
(?FLAGS) Turn a mode on inside the patternApplies from this point to the end of the pattern or the enclosing group.

A flag before | takes the alternatives after it into its scope: a(?i)b|c is read as a(?i:b|c).

test
(?FLAGS) Turn a mode on in the middle of the patternThe mode applies from where the flag stands, not to what comes before it. Some dialects accept an inline flag only at the very start of the pattern. test
(?FLAGS:X) Turn a mode on for one group test
(?-FLAGS) Turn a mode off inside the pattern

A flag before | takes the alternatives after it into its scope: a(?-i)b|c is read as a(?-i:b|c).

test
(?a) ASCII-only classesMakes the shorthand classes such as the digit class match ASCII characters only. test
(?u) Unicode classesMakes the shorthand classes such as the digit class match by Unicode property. test
n/a No automatic capturePlain parentheses group without capturing; only named groups capture. not in Ruby

Tried: (?n)(a)(?<x>b) does not compile.

who has it
n/a Locale-dependent classesClasses and case folding follow the current locale. not in Ruby

The modes are i, m, x and o, plus the encoding modifiers n, u, e and s; none follows the process locale. Matching depends on the encodings of the regexp and the string.

doc Not run on an engine: taken from the documentation.

who has it
n/a Native character set rulesPerl's rules from before 5.14: the pattern and the text decide between native and Unicode rules. not in Ruby

Ruby documents no d modifier. The Onigmo reference does list an inline (?d), called Default (compatible with Ruby 1.9.3), which keeps \w, \d and \s ASCII-only while \b and POSIX brackets follow the encoding; it is a different thing from the Perl mode.

doc Not run on an engine: taken from the documentation.

who has it
POSIX named classes 1 of 1
SyntaxMatchesNotesTry
[[:NAME:]]not[^[:NAME:]] POSIX named classA named set of characters, written inside a bracket expression.

14 names: alnum alpha ascii blank cntrl digit graph lower print punct space upper word xdigit

Not here: nonascii multibyte

test
POSIX collating symbols 0 of 2
SyntaxMatchesNotesTry
n/a Equivalence classEvery character that collates the same as X in the current locale. not in Ruby

Tried: [[=a=]] compiles, and means something else.

who has it
n/a Collating symbol by nameA single collating element, written by its POSIX name inside a bracket expression. not in Ruby

Tried: [[.SOH.]] compiles, and means something else.

who has it
Unicode properties 3 of 4
SyntaxMatchesNotesTry
\p{NAME}not\P{NAME} Unicode general category

36 names: L Ll Lm Lo Lt Lu C Cc Cf Cn Co M Mc Me Mn N Nd Nl No P Pc Pd Pe Pi Pf Po Ps S Sc Sk Sm So Z Zl Zp Zs

test
\p{NAME}not\P{NAME} Unicode script

8 names: Latin Greek Cyrillic Hebrew Arabic Han Hiragana Thai

test
\X Extended grapheme clusterOne character as a reader sees it: a base character with its combining marks. test
n/a Unicode text boundaryA boundary as Unicode's segmentation rules define it, which is not the same as a boundary between word and non-word characters. not in Ruby

Tried: e\b{gcb} compiles, and matches as plain text or not at all.

who has it
Syntax table classes 0 of 2
SyntaxMatchesNotesTry
n/a Syntax classA character with the given role in the syntax table: whitespace, word, symbol, punctuation, a bracket. not in Ruby

Tried: \s- compiles, and matches as plain text or not at all.

who has it
n/a Character categoryA character in one of the editor's character categories, most of which are scripts. not in Ruby

Tried: \ca compiles, and matches as plain text or not at all.

who has it
Host language 10 of 15
SyntaxMatchesNotesTry
/PATTERN/%r{PATTERN} Pattern in source codeHow a pattern is written where it is used: a regex literal with its delimiters, or the kind of string the documentation recommends.

A / inside a slash literal must be backslash-escaped; %r accepts almost any delimiter, including the pairs [], {}, () and <>, which avoids that. The source behaves like a double-quoted string, so #{...} interpolates.

doc Not run on an engine: taken from the documentation.

/PATTERN/FLAGS%r{PATTERN}FLAGSRegexp.new(PATTERN, FLAGS) Flags outside the patternWhere flags such as case-insensitive go when they are not written inside the pattern.

Literal modifiers are i, m, x, o and the encoding letters n, u, e, s. Regexp.new takes a string of option letters or an OR of Regexp::IGNORECASE, Regexp::EXTENDED, Regexp::MULTILINE and Regexp::NOENCODING.

doc Not run on an engine: taken from the documentation.

Regexp.new(PATTERN)/PATTERN/ Compile once and reuseBuild a pattern object and keep it.

Regexp.new builds a Regexp object from a string. A literal is already a Regexp object; one with #{...} interpolation is rebuilt on each evaluation unless it carries the o modifier.

doc Not run on an engine: taken from the documentation.

TEXT =~ /PATTERN/TEXT.match(/PATTERN/)TEXT.match?(/PATTERN/) Search a textTest whether the pattern matches anywhere in a text, or get the first match.

All three search anywhere in the string: =~ returns the offset or nil, match a MatchData or nil, match? true or false. match? does not set $~ and the other match globals.

doc Not run on an engine: taken from the documentation.

TEXT.scan(/PATTERN/) Every matchGet all the matches that do not overlap.

Returns an array of matched substrings, or an array of arrays of group captures when the pattern has groups. With a block, each result is passed to the block instead.

doc Not run on an engine: taken from the documentation.

TEXT.sub(/PATTERN/, REPLACEMENT)TEXT.gsub(/PATTERN/, REPLACEMENT) Replace matches

sub replaces the first match and gsub every match; both return a new string, while sub! and gsub! change TEXT in place. A block can compute the replacement instead.

doc Not run on an engine: taken from the documentation.

$NUMm[NUM]m[:NAME] Captured group, in codeRead what a group captured after a match.

m is the MatchData returned by match, also available as $~. A literal with named groups on the left of =~ also assigns each capture to a local variable of that name, and a regexp with a named capture has no unnamed captures.

doc Not run on an engine: taken from the documentation.

\NUM\k<NAME> Captured group, in replacement text

Only groups 1 to 9 can be written as \NUM, so a digit after it is literal text. A string literal consumes backslashes: in a double-quoted replacement write "\\1".

doc Not run on an engine: taken from the documentation.

\0\& Whole match, in replacement text

$& inside a replacement string is ordinary text; it works only in the block form. \\ in the replacement is a single literal backslash.

doc Not run on an engine: taken from the documentation.

n/a Match once until resetA match operator that succeeds one time only, until it is reset. not in Ruby

No match-once operator: neither the literal syntax nor the Regexp methods remember a previous success.

doc Not run on an engine: taken from the documentation.

who has it
n/a Keep the position after a failed matchIn a repeated search, a failed match does not send the next one back to the start. not in Ruby

A Regexp or String stores no search position; match and match? take an explicit offset argument instead.

doc Not run on an engine: taken from the documentation.

who has it
n/a Keep the matched text for laterAsk for the matched text, and the text around it, to be saved. not in Ruby

Nothing has to be requested: match and =~ always set $~, $& and the pre-match and post-match globals. The cost control runs the other way: match? skips setting them.

doc Not run on an engine: taken from the documentation.

who has it
/PATTERN/o Interpolate and compile only onceA pattern literal with variables in it is built the first time and never again.

The Regexp built the first time a literal with #{...} interpolation is evaluated is saved and reused for every later evaluation. A literal without interpolation behaves that way already.

doc Not run on an engine: taken from the documentation.

n/a Code inside a patternHost-language code that runs when the match reaches it. not in Ruby

The Onigmo reference lists (?{code}) among the Perl features it lacks, and the Ruby Regexp documentation has no such construct. #{...} in a literal runs when the Regexp is built, not during matching.

doc Not run on an engine: taken from the documentation.

who has it
n/a Pattern computed during the matchCode inside the pattern whose result is matched as a pattern at that point. not in Ruby

The Onigmo reference lists (??{code}) as missing. Interpolation with #{...} builds the pattern text before matching starts.

doc Not run on an engine: taken from the documentation.

who has it

Every row was run on the engine named above, except the rows marked doc: those describe something no regex engine can run by itself, such as the syntax of the host language, and are taken from the documentation they link to. For a row marked "not in Ruby", the spelling other dialects use was tried and the line starting "Tried" says what it did here; that shows the spelling does not work, not that no other spelling exists.