Numeric is the class from which all higher-level numeric classes should inherit.
Numeric allows instantiation of heap-allocated objects. Other core numeric classes such as Integer
are implemented as immediates, which means that each Integer
is a single immutable object which is always passed by value.
a = 1 1.object_id == a.object_id #=> true
There can only ever be one instance of the integer 1
, for example. Ruby ensures this by preventing instantiation. If duplication is attempted, the same instance is returned.
Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class 1.dup #=> 1 1.object_id == 1.dup.object_id #=> true
For this reason, Numeric should be used when defining other numeric classes.
Classes which inherit from Numeric must implement coerce
, which returns a two-member Array
containing an object that has been coerced into an instance of the new class and self
(see coerce
).
Inheriting classes should also implement arithmetic operator methods (+
, -
, *
and /
) and the <=>
operator (see Comparable
). These methods may rely on coerce
to ensure interoperability with instances of other numeric classes.
class Tally < Numeric def initialize(string) @string = string end def to_s @string end def to_i @string.size end def coerce(other) [self.class.new('|' * other.to_i), self] end def <=>(other) to_i <=> other.to_i end def +(other) self.class.new('|' * (to_i + other.to_i)) end def -(other) self.class.new('|' * (to_i - other.to_i)) end def *(other) self.class.new('|' * (to_i * other.to_i)) end def /(other) self.class.new('|' * (to_i / other.to_i)) end end tally = Tally.new('||') puts tally * 2 #=> "||||" puts tally > 1 #=> true
First, what’s elsewhere. Class Numeric:
Inherits from class Object.
Includes module Comparable.
Here, class Numeric provides methods for:
finite?
: Returns true unless self
is infinite or not a number.
infinite?
: Returns -1, nil
or +1, depending on whether self
is -Infinity<tt>, finite, or <tt>+Infinity
.
integer?
: Returns whether self
is an integer.
negative?
: Returns whether self
is negative.
nonzero?
: Returns whether self
is not zero.
positive?
: Returns whether self
is positive.
real?
: Returns whether self
is a real value.
zero?
: Returns whether self
is zero.
<=>
: Returns:
-1 if self
is less than the given value.
0 if self
is equal to the given value.
1 if self
is greater than the given value.
nil
if self
and the given value are not comparable.
eql?
: Returns whether self
and the given value have the same value and type.
%
(aliased as modulo
): Returns the remainder of self
divided by the given value.
-@
: Returns the value of self
, negated.
abs
(aliased as magnitude
): Returns the absolute value of self
.
abs2
: Returns the square of self
.
angle
(aliased as arg
and phase
): Returns 0 if self
is positive, Math::PI otherwise.
ceil
: Returns the smallest number greater than or equal to self
, to a given precision.
coerce
: Returns array [coerced_self, coerced_other]
for the given other value.
conj
(aliased as conjugate
): Returns the complex conjugate of self
.
denominator
: Returns the denominator (always positive) of the Rational
representation of self
.
div
: Returns the value of self
divided by the given value and converted to an integer.
divmod
: Returns array [quotient, modulus]
resulting from dividing self
the given divisor.
fdiv
: Returns the Float
result of dividing self
by the given divisor.
floor
: Returns the largest number less than or equal to self
, to a given precision.
i
: Returns the Complex
object Complex(0, self)
. the given value.
imaginary
(aliased as imag
): Returns the imaginary part of the self
.
numerator
: Returns the numerator of the Rational
representation of self
; has the same sign as self
.
polar
: Returns the array [self.abs, self.arg]
.
quo
: Returns the value of self
divided by the given value.
real
: Returns the real part of self
.
rect
(aliased as rectangular
): Returns the array [self, 0]
.
remainder
: Returns self-arg*(self/arg).truncate
for the given arg
.
round
: Returns the value of self
rounded to the nearest value for the given a precision.
to_int
: Returns the Integer
representation of self
, truncating if necessary.
truncate
: Returns self
truncated (toward zero) to a given precision.
A Float object represents a sometimes-inexact real number using the native architecture’s double-precision floating point representation.
Floating point has a different arithmetic and is an inexact number. So you should know its esoteric system. See following:
You can create a Float object explicitly with:
A floating-point literal.
You can convert certain objects to Floats with:
Method Float
.
First, what’s elsewhere. Class Float:
Inherits from class Numeric.
Here, class Float provides methods for:
finite?
: Returns whether self
is finite.
hash
: Returns the integer hash code for self
.
infinite?
: Returns whether self
is infinite.
nan?
: Returns whether self
is a NaN (not-a-number).
<
: Returns whether self
is less than the given value.
<=
: Returns whether self
is less than or equal to the given value.
<=>
: Returns a number indicating whether self
is less than, equal to, or greater than the given value.
==
(aliased as ===
and eql?
): Returns whether self
is equal to the given value.
>
: Returns whether self
is greater than the given value.
>=
: Returns whether self
is greater than or equal to the given value.
*
: Returns the product of self
and the given value.
**
: Returns the value of self
raised to the power of the given value.
+
: Returns the sum of self
and the given value.
-
: Returns the difference of self
and the given value.
/
: Returns the quotient of self
and the given value.
ceil
: Returns the smallest number greater than or equal to self
.
coerce
: Returns a 2-element array containing the given value converted to a Float and self
divmod
: Returns a 2-element array containing the quotient and remainder results of dividing self
by the given value.
fdiv
: Returns the Float result of dividing self
by the given value.
floor
: Returns the greatest number smaller than or equal to self
.
next_float
: Returns the next-larger representable Float.
prev_float
: Returns the next-smaller representable Float.
quo
: Returns the quotient from dividing self
by the given value.
round
: Returns self
rounded to the nearest value, to a given precision.
to_i
(aliased as to_int
): Returns self
truncated to an Integer
.
to_s
(aliased as inspect
): Returns a string containing the place-value representation of self
in the given radix.
truncate
: Returns self
truncated to a given precision.
Fibers are primitives for implementing light weight cooperative concurrency in Ruby. Basically they are a means of creating code blocks that can be paused and resumed, much like threads. The main difference is that they are never preempted and that the scheduling must be done by the programmer and not the VM.
As opposed to other stackless light weight concurrency models, each fiber comes with a stack. This enables the fiber to be paused from deeply nested function calls within the fiber block. See the ruby(1) manpage to configure the size of the fiber stack(s).
When a fiber is created it will not run automatically. Rather it must be explicitly asked to run using the Fiber#resume
method. The code running inside the fiber can give up control by calling Fiber.yield
in which case it yields control back to caller (the caller of the Fiber#resume
).
Upon yielding or termination the Fiber
returns the value of the last executed expression
For instance:
fiber = Fiber.new do Fiber.yield 1 2 end puts fiber.resume puts fiber.resume puts fiber.resume
produces
1 2 FiberError: dead fiber called
The Fiber#resume
method accepts an arbitrary number of parameters, if it is the first call to resume
then they will be passed as block arguments. Otherwise they will be the return value of the call to Fiber.yield
Example:
fiber = Fiber.new do |first| second = Fiber.yield first + 2 end puts fiber.resume 10 puts fiber.resume 1_000_000 puts fiber.resume "The fiber will be dead before I can cause trouble"
produces
12 1000000 FiberError: dead fiber called
The concept of non-blocking fiber was introduced in Ruby 3.0. A non-blocking fiber, when reaching a operation that would normally block the fiber (like sleep
, or wait for another process or I/O) will yield control to other fibers and allow the scheduler to handle blocking and waking up (resuming) this fiber when it can proceed.
For a Fiber
to behave as non-blocking, it need to be created in Fiber.new
with blocking: false
(which is the default), and Fiber.scheduler
should be set with Fiber.set_scheduler
. If Fiber.scheduler
is not set in the current thread, blocking and non-blocking fibers’ behavior is identical.
Ruby doesn’t provide a scheduler class: it is expected to be implemented by the user and correspond to Fiber::Scheduler
.
There is also Fiber.schedule
method, which is expected to immediately perform the given block in a non-blocking manner. Its actual implementation is up to the scheduler.
A class which allows both internal and external iteration.
An Enumerator
can be created by the following methods.
Most methods have two forms: a block form where the contents are evaluated for each item in the enumeration, and a non-block form which returns a new Enumerator
wrapping the iteration.
enumerator = %w(one two three).each puts enumerator.class # => Enumerator enumerator.each_with_object("foo") do |item, obj| puts "#{obj}: #{item}" end # foo: one # foo: two # foo: three enum_with_obj = enumerator.each_with_object("foo") puts enum_with_obj.class # => Enumerator enum_with_obj.each do |item, obj| puts "#{obj}: #{item}" end # foo: one # foo: two # foo: three
This allows you to chain Enumerators together. For example, you can map a list’s elements to strings containing the index and the element as a string via:
puts %w[foo bar baz].map.with_index { |w, i| "#{i}:#{w}" } # => ["0:foo", "1:bar", "2:baz"]
An Enumerator
can also be used as an external iterator. For example, Enumerator#next
returns the next value of the iterator or raises StopIteration
if the Enumerator
is at the end.
e = [1,2,3].each # returns an enumerator object. puts e.next # => 1 puts e.next # => 2 puts e.next # => 3 puts e.next # raises StopIteration
next
, next_values
, peek
, and peek_values
are the only methods which use external iteration (and Array#zip(Enumerable-not-Array)
which uses next
internally).
These methods do not affect other internal enumeration methods, unless the underlying iteration method itself has side-effect, e.g. IO#each_line
.
FrozenError
will be raised if these methods are called against a frozen enumerator. Since rewind
and feed
also change state for external iteration, these methods may raise FrozenError
too.
External iteration differs significantly from internal iteration due to using a Fiber:
The Fiber
adds some overhead compared to internal enumeration.
The stacktrace will only include the stack from the Enumerator
, not above.
Fiber-local variables are not inherited inside the Enumerator
Fiber
, which instead starts with no Fiber-local variables.
Fiber
storage variables are inherited and are designed to handle Enumerator
Fibers. Assigning to a Fiber
storage variable only affects the current Fiber
, so if you want to change state in the caller Fiber
of the Enumerator
Fiber
, you need to use an extra indirection (e.g., use some object in the Fiber
storage variable and mutate some ivar of it).
Concretely:
Thread.current[:fiber_local] = 1 Fiber[:storage_var] = 1 e = Enumerator.new do |y| p Thread.current[:fiber_local] # for external iteration: nil, for internal iteration: 1 p Fiber[:storage_var] # => 1, inherited Fiber[:storage_var] += 1 y << 42 end p e.next # => 42 p Fiber[:storage_var] # => 1 (it ran in a different Fiber) e.each { p _1 } p Fiber[:storage_var] # => 2 (it ran in the same Fiber/"stack" as the current Fiber)
You can use an external iterator to implement an internal iterator as follows:
def ext_each(e) while true begin vs = e.next_values rescue StopIteration return $!.result end y = yield(*vs) e.feed y end end o = Object.new def o.each puts yield puts yield(1) puts yield(1, 2) 3 end # use o.each as an internal iterator directly. puts o.each {|*x| puts x; [:b, *x] } # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3 # convert o.each to an external iterator for # implementing an internal iterator. puts ext_each(o.to_enum) {|*x| puts x; [:b, *x] } # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
Raised to stop the iteration, in particular by Enumerator#next
. It is rescued by Kernel#loop
.
loop do puts "Hello" raise StopIteration puts "World" end puts "Done!"
produces:
Hello Done!
Raised when the interrupt signal is received, typically because the user has pressed Control-C (on most posix platforms). As such, it is a subclass of SignalException
.
begin puts "Press ctrl-C when you get bored" loop {} rescue Interrupt => e puts "Note: You will typically use Signal.trap instead." end
produces:
Press ctrl-C when you get bored
then waits until it is interrupted with Control-C and then prints:
Note: You will typically use Signal.trap instead.
The most standard error types are subclasses of StandardError
. A rescue clause without an explicit Exception
class will rescue all StandardErrors (and only those).
def foo raise "Oups" end foo rescue "Hello" #=> "Hello"
On the other hand:
require 'does/not/exist' rescue "Hi"
raises the exception:
LoadError: no such file to load -- does/not/exist
Raised when encountering an object that is not of the expected type.
[1, 2, 3].first("two")
raises the exception:
TypeError: no implicit conversion of String into Integer
Raised when the arguments are wrong and there isn’t a more specific Exception
class.
Ex: passing the wrong number of arguments
[1, 2, 3].first(4, 5)
raises the exception:
ArgumentError: wrong number of arguments (given 2, expected 1)
Ex: passing an argument that is not acceptable:
[1, 2, 3].first(-4)
raises the exception:
ArgumentError: negative array size
Raised when the given index is invalid.
a = [:foo, :bar] a.fetch(0) #=> :foo a[4] #=> nil a.fetch(4) #=> IndexError: index 4 outside of array bounds: -2...2
Raised when the specified key is not found. It is a subclass of IndexError
.
h = {"foo" => :bar} h.fetch("foo") #=> :bar h.fetch("baz") #=> KeyError: key not found: "baz"
ScriptError
is the superclass for errors raised when a script can not be executed because of a LoadError
, NotImplementedError
or a SyntaxError
. Note these type of ScriptErrors
are not StandardError
and will not be rescued unless it is specified explicitly (or its ancestor Exception
).
Raised when encountering Ruby code with an invalid syntax.
eval("1+1=2")
raises the exception:
SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
Raised when a feature is not implemented on the current platform. For example, methods depending on the fsync
or fork
system calls may raise this exception if the underlying operating system or Ruby runtime does not support them.
Note that if fork
raises a NotImplementedError
, then respond_to?(:fork)
returns false
.
Raised when a given name is invalid or undefined.
puts foo
raises the exception:
NameError: undefined local variable or method `foo' for main:Object
Since constant names must start with a capital:
Integer.const_set :answer, 42
raises the exception:
NameError: wrong constant name answer
Raised when a method is called on a receiver which doesn’t have it defined and also fails to respond with method_missing
.
"hello".to_ary
raises the exception:
NoMethodError: undefined method `to_ary' for an instance of String
A generic error class raised when an invalid operation is attempted. Kernel#raise
will raise a RuntimeError
if no Exception
class is specified.
raise "ouch"
raises the exception:
RuntimeError: ouch
Raised when there is an attempt to modify a frozen object.
[1, 2, 3].freeze << 4
raises the exception:
FrozenError: can't modify frozen Array
No longer used by internal code.
Raised when memory allocation fails.
SystemCallError
is the base class for all low-level platform-dependent errors.
The errors available on the current platform are subclasses of SystemCallError
and are defined in the Errno
module.
File.open("does/not/exist")
raises the exception:
Errno::ENOENT: No such file or directory - does/not/exist
A Range object represents a collection of values that are between given begin and end values.
You can create an Range object explicitly with:
A range literal:
# Ranges that use '..' to include the given end value. (1..4).to_a # => [1, 2, 3, 4] ('a'..'d').to_a # => ["a", "b", "c", "d"] # Ranges that use '...' to exclude the given end value. (1...4).to_a # => [1, 2, 3] ('a'...'d').to_a # => ["a", "b", "c"]
A range may be created using method Range.new
:
# Ranges that by default include the given end value. Range.new(1, 4).to_a # => [1, 2, 3, 4] Range.new('a', 'd').to_a # => ["a", "b", "c", "d"] # Ranges that use third argument +exclude_end+ to exclude the given end value. Range.new(1, 4, true).to_a # => [1, 2, 3] Range.new('a', 'd', true).to_a # => ["a", "b", "c"]
A beginless range has a definite end value, but a nil
begin value. Such a range includes all values up to the end value.
r = (..4) # => nil..4 r.begin # => nil r.include?(-50) # => true r.include?(4) # => true r = (...4) # => nil...4 r.include?(4) # => false Range.new(nil, 4) # => nil..4 Range.new(nil, 4, true) # => nil...4
A beginless range may be used to slice an array:
a = [1, 2, 3, 4] r = (..2) # => nil...2 a[r] # => [1, 2]
Method each
for a beginless range raises an exception.
An endless range has a definite begin value, but a nil
end value. Such a range includes all values from the begin value.
r = (1..) # => 1.. r.end # => nil r.include?(50) # => true Range.new(1, nil) # => 1..
The literal for an endless range may be written with either two dots or three. The range has the same elements, either way. But note that the two are not equal:
r0 = (1..) # => 1.. r1 = (1...) # => 1... r0.begin == r1.begin # => true r0.end == r1.end # => true r0 == r1 # => false
An endless range may be used to slice an array:
a = [1, 2, 3, 4] r = (2..) # => 2.. a[r] # => [3, 4]
Method each
for an endless range calls the given block indefinitely:
a = [] r = (1..) r.each do |i| a.push(i) if i.even? break if i > 10 end a # => [2, 4, 6, 8, 10]
A range can be both beginless and endless. For literal beginless, endless ranges, at least the beginning or end of the range must be given as an explicit nil value. It is recommended to use an explicit nil beginning and implicit nil end, since that is what Ruby uses for Range#inspect
:
(nil..) # => (nil..) (..nil) # => (nil..) (nil..nil) # => (nil..)
An object may be put into a range if its class implements instance method <=>
. Ruby core classes that do so include Array
, Complex
, File::Stat
, Float
, Integer
, Kernel
, Module
, Numeric
, Rational
, String
, Symbol
, and Time
.
Example:
t0 = Time.now # => 2021-09-19 09:22:48.4854986 -0500 t1 = Time.now # => 2021-09-19 09:22:56.0365079 -0500 t2 = Time.now # => 2021-09-19 09:23:08.5263283 -0500 (t0..t2).include?(t1) # => true (t0..t1).include?(t2) # => false
A range can be iterated over only if its elements implement instance method succ
. Ruby core classes that do so include Integer
, String
, and Symbol
(but not the other classes mentioned above).
Iterator methods include:
Included from module Enumerable: each_entry
, each_with_index
, each_with_object
, each_slice
, each_cons
, and reverse_each
.
Example:
a = [] (1..4).each {|i| a.push(i) } a # => [1, 2, 3, 4]
A user-defined class that is to be used in a range must implement instance <=>
; see Integer#<=>
. To make iteration available, it must also implement instance method succ
; see Integer#succ
.
The class below implements both <=>
and succ
, and so can be used both to construct ranges and to iterate over them. Note that the Comparable
module is included so the ==
method is defined in terms of <=>
.
# Represent a string of 'X' characters. class Xs include Comparable attr_accessor :length def initialize(n) @length = n end def succ Xs.new(@length + 1) end def <=>(other) @length <=> other.length end def to_s sprintf "%2d #{inspect}", @length end def inspect 'X' * @length end end r = Xs.new(3)..Xs.new(6) #=> XXX..XXXXXX r.to_a #=> [XXX, XXXX, XXXXX, XXXXXX] r.include?(Xs.new(5)) #=> true r.include?(Xs.new(7)) #=> false
First, what’s elsewhere. Class Range:
Inherits from class Object.
Includes module Enumerable, which provides dozens of additional methods.
Here, class Range provides methods that are useful for:
::new
: Returns a new range.
begin
: Returns the begin value given for self
.
bsearch
: Returns an element from self
selected by a binary search.
count
: Returns a count of elements in self
.
end
: Returns the end value given for self
.
exclude_end?
: Returns whether the end object is excluded.
first
: Returns the first elements of self
.
hash
: Returns the integer hash code.
last
: Returns the last elements of self
.
max
: Returns the maximum values in self
.
min
: Returns the minimum values in self
.
minmax
: Returns the minimum and maximum values in self
.
size
: Returns the count of elements in self
.
==
: Returns whether a given object is equal to self
(uses ==
).
===
: Returns whether the given object is between the begin and end values.
cover?
: Returns whether a given object is within self
.
eql?
: Returns whether a given object is equal to self
(uses eql?
).
include?
(aliased as member?
): Returns whether a given object is an element of self
.
%
: Requires argument n
; calls the block with each n
-th element of self
.
each
: Calls the block with each element of self
.
step
: Takes optional argument n
(defaults to 1); calls the block with each n
-th element of self
.
inspect
: Returns a string representation of self
(uses inspect
).
to_a
(aliased as entries
): Returns elements of self
in an array.
::json_create
: Returns a new Range object constructed from the given object.
as_json
: Returns a 2-element hash representing self
.
to_json
: Returns a JSON string representing self
.
To make these methods available:
require 'json/add/range'
A regular expression (also called a regexp) is a match pattern (also simply called a pattern).
A common notation for a regexp uses enclosing slash characters:
/foo/
A regexp may be applied to a target string; The part of the string (if any) that matches the pattern is called a match, and may be said to match:
re = /red/ re.match?('redirect') # => true # Match at beginning of target. re.match?('bored') # => true # Match at end of target. re.match?('credit') # => true # Match within target. re.match?('foo') # => false # No match.
A regexp may be used:
To extract substrings based on a given pattern:
re = /foo/ # => /foo/ re.match('food') # => #<MatchData "foo"> re.match('good') # => nil
See sections Method match and Operator =~.
To determine whether a string matches a given pattern:
re.match?('food') # => true re.match?('good') # => false
See section Method match?.
As an argument for calls to certain methods in other classes and modules; most such methods accept an argument that may be either a string or the (much more powerful) regexp.
See Regexp Methods.
A regexp object has:
A source; see Sources.
Several modes; see Modes.
A timeout; see Timeouts.
An encoding; see Encodings.
A regular expression may be created with:
A regexp literal using slash characters (see Regexp Literals):
# This is a very common usage. /foo/ # => /foo/
A %r
regexp literal (see Regexp Literals):
# Same delimiter character at beginning and end; # useful for avoiding escaping characters %r/name\/value pair/ # => /name\/value pair/ %r:name/value pair: # => /name\/value pair/ %r|name/value pair| # => /name\/value pair/ # Certain "paired" characters can be delimiters. %r[foo] # => /foo/ %r{foo} # => /foo/ %r(foo) # => /foo/ %r<foo> # => /foo/
Method Regexp.new
.
match
Each of the methods Regexp#match
, String#match
, and Symbol#match
returns a MatchData
object if a match was found, nil
otherwise; each also sets global variables:
'food'.match(/foo/) # => #<MatchData "foo"> 'food'.match(/bar/) # => nil
=~
Each of the operators Regexp#=~
, String#=~
, and Symbol#=~
returns an integer offset if a match was found, nil
otherwise; each also sets global variables:
/bar/ =~ 'foo bar' # => 4 'foo bar' =~ /bar/ # => 4 /baz/ =~ 'foo bar' # => nil
match?
Each of the methods Regexp#match?
, String#match?
, and Symbol#match?
returns true
if a match was found, false
otherwise; none sets global variables:
'food'.match?(/foo/) # => true 'food'.match?(/bar/) # => false
Certain regexp-oriented methods assign values to global variables:
#match
: see Method match.
#=~
: see Operator =~.
The affected global variables are:
$~
: Returns a MatchData
object, or nil
.
$&
: Returns the matched part of the string, or nil
.
$`
: Returns the part of the string to the left of the match, or nil
.
$'
: Returns the part of the string to the right of the match, or nil
.
$+
: Returns the last group matched, or nil
.
$1
, $2
, etc.: Returns the first, second, etc., matched group, or nil
. Note that $0
is quite different; it returns the name of the currently executing program.
Examples:
# Matched string, but no matched groups. 'foo bar bar baz'.match('bar') $~ # => #<MatchData "bar"> $& # => "bar" $` # => "foo " $' # => " bar baz" $+ # => nil $1 # => nil # Matched groups. /s(\w{2}).*(c)/.match('haystack') $~ # => #<MatchData "stac" 1:"ta" 2:"c"> $& # => "stac" $` # => "hay" $' # => "k" $+ # => "c" $1 # => "ta" $2 # => "c" $3 # => nil # No match. 'foo'.match('bar') $~ # => nil $& # => nil $` # => nil $' # => nil $+ # => nil $1 # => nil
Note that Regexp#match?
, String#match?
, and Symbol#match?
do not set global variables.
As seen above, the simplest regexp uses a literal expression as its source:
re = /foo/ # => /foo/ re.match('food') # => #<MatchData "foo"> re.match('good') # => nil
A rich collection of available subexpressions gives the regexp great power and flexibility:
Regexp special characters, called metacharacters, have special meanings in certain contexts; depending on the context, these are sometimes metacharacters:
. ? - + * ^ \ | $ ( ) [ ] { }
To match a metacharacter literally, backslash-escape it:
# Matches one or more 'o' characters. /o+/.match('foo') # => #<MatchData "oo"> # Would match 'o+'. /o\+/.match('foo') # => nil
To match a backslash literally, backslash-escape it:
/\./.match('\.') # => #<MatchData "."> /\\./.match('\.') # => #<MatchData "\\.">
Method
Regexp.escape
returns an escaped string:
Regexp.escape('.?-+*^\|$()[]{}') # => "\\.\\?\\-\\+\\*\\^\\\\\\|\\$\\(\\)\\[\\]\\{\\}"
The source literal largely behaves like a double-quoted string; see Regexp Literals.
In particular, a source literal may contain interpolated expressions:
s = 'foo' # => "foo" /#{s}/ # => /foo/ /#{s.capitalize}/ # => /Foo/ /#{2 + 2}/ # => /4/
There are differences between an ordinary string literal and a source literal; see Shorthand Character Classes.
\s
in an ordinary string literal is equivalent to a space character; in a source literal, it’s shorthand for matching a whitespace character.
In an ordinary string literal, these are (needlessly) escaped characters; in a source literal, they are shorthands for various matching characters:
\w \W \d \D \h \H \S \R
A character class is delimited by square brackets; it specifies that certain characters match at a given point in the target string:
# This character class will match any vowel. re = /B[aeiou]rd/ re.match('Bird') # => #<MatchData "Bird"> re.match('Bard') # => #<MatchData "Bard"> re.match('Byrd') # => nil
A character class may contain hyphen characters to specify ranges of characters:
# These regexps have the same effect. /[abcdef]/.match('foo') # => #<MatchData "f"> /[a-f]/.match('foo') # => #<MatchData "f"> /[a-cd-f]/.match('foo') # => #<MatchData "f">
When the first character of a character class is a caret (^
), the sense of the class is inverted: it matches any character except those specified.
/[^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]/
Each of the following metacharacters serves as a shorthand for a character class:
/./
: Matches any character except a newline:
/./.match('foo') # => #<MatchData "f"> /./.match("\n") # => nil
/./m
: Matches any character, including a newline; see Multiline Mode:
/./m.match("\n") # => #<MatchData "\n">
/\w/
: Matches a word character: equivalent to [a-zA-Z0-9_]
:
/\w/.match(' foo') # => #<MatchData "f"> /\w/.match(' _') # => #<MatchData "_"> /\w/.match(' ') # => nil
/\W/
: Matches a non-word character: equivalent to [^a-zA-Z0-9_]
:
/\W/.match(' ') # => #<MatchData " "> /\W/.match('_') # => nil
/\d/
: Matches a digit character: equivalent to [0-9]
:
/\d/.match('THX1138') # => #<MatchData "1"> /\d/.match('foo') # => nil
/\D/
: Matches a non-digit character: equivalent to [^0-9]
:
/\D/.match('123Jump!') # => #<MatchData "J"> /\D/.match('123') # => nil
/\h/
: Matches a hexdigit character: equivalent to [0-9a-fA-F]
:
/\h/.match('xyz fedcba9876543210') # => #<MatchData "f"> /\h/.match('xyz') # => nil
/\H/
: Matches a non-hexdigit character: equivalent to [^0-9a-fA-F]
:
/\H/.match('fedcba9876543210xyz') # => #<MatchData "x"> /\H/.match('fedcba9876543210') # => nil
/\s/
: Matches a whitespace character: equivalent to /[ \t\r\n\f\v]/
:
/\s/.match('foo bar') # => #<MatchData " "> /\s/.match('foo') # => nil
/\S/
: Matches a non-whitespace character: equivalent to /[^ \t\r\n\f\v]/
:
/\S/.match(" \t\r\n\f\v foo") # => #<MatchData "f"> /\S/.match(" \t\r\n\f\v") # => nil
/\R/
: Matches a linebreak, platform-independently:
/\R/.match("\r") # => #<MatchData "\r"> # Carriage return (CR) /\R/.match("\n") # => #<MatchData "\n"> # Newline (LF) /\R/.match("\f") # => #<MatchData "\f"> # Formfeed (FF) /\R/.match("\v") # => #<MatchData "\v"> # Vertical tab (VT) /\R/.match("\r\n") # => #<MatchData "\r\n"> # CRLF /\R/.match("\u0085") # => #<MatchData "\u0085"> # Next line (NEL) /\R/.match("\u2028") # => #<MatchData "\u2028"> # Line separator (LSEP) /\R/.match("\u2029") # => #<MatchData "\u2029"> # Paragraph separator (PSEP)
An anchor is a metasequence that matches a zero-width position between characters in the target string.
For a subexpression with no anchor, matching may begin anywhere in the target string:
/real/.match('surrealist') # => #<MatchData "real">
For a subexpression with an anchor, matching must begin at the matched anchor.
Each of these anchors matches a boundary:
^
: Matches the beginning of a line:
/^bar/.match("foo\nbar") # => #<MatchData "bar"> /^ar/.match("foo\nbar") # => nil
$
: Matches the end of a line:
/bar$/.match("foo\nbar") # => #<MatchData "bar"> /ba$/.match("foo\nbar") # => nil
\A
: Matches the beginning of the string:
/\Afoo/.match('foo bar') # => #<MatchData "foo"> /\Afoo/.match(' foo bar') # => nil
\Z
: Matches the end of the string; if string ends with a single newline, it matches just before the ending newline:
/foo\Z/.match('bar foo') # => #<MatchData "foo"> /foo\Z/.match('foo bar') # => nil /foo\Z/.match("bar foo\n") # => #<MatchData "foo"> /foo\Z/.match("bar foo\n\n") # => nil
\z
: Matches the end of the string:
/foo\z/.match('bar foo') # => #<MatchData "foo"> /foo\z/.match('foo bar') # => nil /foo\z/.match("bar foo\n") # => nil
\b
: Matches word boundary when not inside brackets; matches backspace ("0x08"
) when inside brackets:
/foo\b/.match('foo bar') # => #<MatchData "foo"> /foo\b/.match('foobar') # => nil
\B
: Matches non-word boundary:
/foo\B/.match('foobar') # => #<MatchData "foo"> /foo\B/.match('foo bar') # => nil
\G
: Matches first matching position:
In methods like String#gsub
and String#scan
, it changes on each iteration. It initially matches the beginning of subject, and in each following iteration it matches where the last match finished.
" a b c".gsub(/ /, '_') # => "____a_b_c" " a b c".gsub(/\G /, '_') # => "____a b c"
In methods like Regexp#match
and String#match
that take an optional offset, it matches where the search begins.
"hello, world".match(/,/, 3) # => #<MatchData ","> "hello, world".match(/\G,/, 3) # => nil
Lookahead anchors:
(?=pat)
: Positive lookahead assertion: ensures that the following characters match pat, but doesn’t include those characters in the matched substring.
(?!pat)
: Negative lookahead assertion: ensures that the following characters do not match pat, but doesn’t include those characters in the matched substring.
Lookbehind anchors:
(?<=pat)
: Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn’t include those characters in the matched substring.
(?<!pat)
: Negative lookbehind assertion: ensures that the preceding characters do not match pat, but doesn’t include those characters in the matched substring.
The pattern below uses positive lookahead and positive lookbehind to match text appearing in … tags without including the tags in the match:
/(?<=<b>)\w+(?=<\/b>)/.match("Fortune favors the <b>bold</b>.") # => #<MatchData "bold">
\K
: Match reset: the matched content preceding \K
in the regexp is excluded from the result. For example, the following two regexps are almost equivalent:
/ab\Kc/.match('abc') # => #<MatchData "c"> /(?<=ab)c/.match('abc') # => #<MatchData "c">
These match same string and $&
equals 'c'
, while the matched position is different.
As are the following two regexps:
/(a)\K(b)\Kc/ /(?<=(?<=(a))(b))c/
The vertical bar metacharacter (|
) may be used within parentheses to express alternation: two or more subexpressions any of which may match the target string.
Two alternatives:
re = /(a|b)/ re.match('foo') # => nil re.match('bar') # => #<MatchData "b" 1:"b">
Four alternatives:
re = /(a|b|c|d)/ re.match('shazam') # => #<MatchData "a" 1:"a"> re.match('cold') # => #<MatchData "c" 1:"c">
Each alternative is a subexpression, and may be composed of other subexpressions:
re = /([a-c]|[x-z])/ re.match('bar') # => #<MatchData "b" 1:"b"> re.match('ooz') # => #<MatchData "z" 1:"z">
Method Regexp.union
provides a convenient way to construct a regexp with alternatives.
A simple regexp matches one character:
/\w/.match('Hello') # => #<MatchData "H">
An added quantifier specifies how many matches are required or allowed:
*
- Matches zero or more times:
/\w*/.match('') # => #<MatchData ""> /\w*/.match('x') # => #<MatchData "x"> /\w*/.match('xyz') # => #<MatchData "yz">
+
- Matches one or more times:
/\w+/.match('') # => nil /\w+/.match('x') # => #<MatchData "x"> /\w+/.match('xyz') # => #<MatchData "xyz">
?
- Matches zero or one times:
/\w?/.match('') # => #<MatchData ""> /\w?/.match('x') # => #<MatchData "x"> /\w?/.match('xyz') # => #<MatchData "x">
{
n}
- Matches exactly n times:
/\w{2}/.match('') # => nil /\w{2}/.match('x') # => nil /\w{2}/.match('xyz') # => #<MatchData "xy">
{
min,}
- Matches min or more times:
/\w{2,}/.match('') # => nil /\w{2,}/.match('x') # => nil /\w{2,}/.match('xy') # => #<MatchData "xy"> /\w{2,}/.match('xyz') # => #<MatchData "xyz">
{,
max}
- Matches max or fewer times:
/\w{,2}/.match('') # => #<MatchData ""> /\w{,2}/.match('x') # => #<MatchData "x"> /\w{,2}/.match('xyz') # => #<MatchData "xy">
{
min,
max}
- Matches at least min times and at most max times:
/\w{1,2}/.match('') # => nil /\w{1,2}/.match('x') # => #<MatchData "x"> /\w{1,2}/.match('xyz') # => #<MatchData "xy">
Quantifier matching may be greedy, lazy, or possessive:
In greedy matching, as many occurrences as possible are matched while still allowing the overall match to succeed. Greedy quantifiers: *
, +
, ?
, {min, max}
and its variants.
In lazy matching, the minimum number of occurrences are matched. Lazy quantifiers: *?
, +?
, ??
, {min, max}?
and its variants.
In possessive matching, once a match is found, there is no backtracking; that match is retained, even if it jeopardises the overall match. Possessive quantifiers: *+
, ++
, ?+
. Note that {min, max}
and its variants do not support possessive matching.
More:
About greedy and lazy matching, see Choosing Minimal or Maximal Repetition.
About possessive matching, see Eliminate Needless Backtracking.
A simple regexp has (at most) one match:
re = /\d\d\d\d-\d\d-\d\d/ re.match('1943-02-04') # => #<MatchData "1943-02-04"> re.match('1943-02-04').size # => 1 re.match('foo') # => nil
Adding one or more pairs of parentheses, (subexpression)
, defines groups, which may result in multiple matched substrings, called captures:
re = /(\d\d\d\d)-(\d\d)-(\d\d)/ re.match('1943-02-04') # => #<MatchData "1943-02-04" 1:"1943" 2:"02" 3:"04"> re.match('1943-02-04').size # => 4
The first capture is the entire matched string; the other captures are the matched substrings from the groups.
A group may have a quantifier:
re = /July 4(th)?/ re.match('July 4') # => #<MatchData "July 4" 1:nil> re.match('July 4th') # => #<MatchData "July 4th" 1:"th"> re = /(foo)*/ re.match('') # => #<MatchData "" 1:nil> re.match('foo') # => #<MatchData "foo" 1:"foo"> re.match('foofoo') # => #<MatchData "foofoo" 1:"foo"> re = /(foo)+/ re.match('') # => nil re.match('foo') # => #<MatchData "foo" 1:"foo"> re.match('foofoo') # => #<MatchData "foofoo" 1:"foo">
The returned MatchData object gives access to the matched substrings:
re = /(\d\d\d\d)-(\d\d)-(\d\d)/ md = re.match('1943-02-04') # => #<MatchData "1943-02-04" 1:"1943" 2:"02" 3:"04"> md[0] # => "1943-02-04" md[1] # => "1943" md[2] # => "02" md[3] # => "04"
A group may be made non-capturing; it is still a group (and, for example, can have a quantifier), but its matching substring is not included among the captures.
A non-capturing group begins with ?:
(inside the parentheses):
# Don't capture the year. re = /(?:\d\d\d\d)-(\d\d)-(\d\d)/ md = re.match('1943-02-04') # => #<MatchData "1943-02-04" 1:"02" 2:"04">
A group match may also be referenced within the regexp itself; such a reference is called a backreference
:
/[csh](..) [csh]\1 in/.match('The cat sat in the hat') # => #<MatchData "cat sat in" 1:"at">
This table shows how each subexpression in the regexp above matches a substring in the target string:
| Subexpression in Regexp | Matching Substring in Target String | |---------------------------|-------------------------------------| | First '[csh]' | Character 'c' | | '(..)' | First substring 'at' | | First space ' ' | First space character ' ' | | Second '[csh]' | Character 's' | | '\1' (backreference 'at') | Second substring 'at' | | ' in' | Substring ' in' |
A regexp may contain any number of groups:
For a large number of groups:
The ordinary \n
notation applies only for n in range (1..9).
The MatchData[n]
notation applies for any non-negative n.
\0
is a special backreference, referring to the entire matched string; it may not be used within the regexp itself, but may be used outside it (for example, in a substitution method call):
'The cat sat in the hat'.gsub(/[csh]at/, '\0s') # => "The cats sats in the hats"
As seen above, a capture can be referred to by its number. A capture can also have a name, prefixed as ?<name>
or ?'name'
, and the name (symbolized) may be used as an index in MatchData[]
:
md = /\$(?<dollars>\d+)\.(?'cents'\d+)/.match("$3.67") # => #<MatchData "$3.67" dollars:"3" cents:"67"> md[:dollars] # => "3" md[:cents] # => "67" # The capture numbers are still valid. md[2] # => "67"
When a regexp contains a named capture, there are no unnamed captures:
/\$(?<dollars>\d+)\.(\d+)/.match("$3.67") # => #<MatchData "$3.67" dollars:"3">
A named group may be backreferenced as \k<name>
:
/(?<vowel>[aeiou]).\k<vowel>.\k<vowel>/.match('ototomy') # => #<MatchData "ototo" vowel:"o">
When (and only when) a regexp contains named capture groups and appears before the =~
operator, the captured substrings are assigned to local variables with corresponding names:
/\$(?<dollars>\d+)\.(?<cents>\d+)/ =~ '$3.67' dollars # => "3" cents # => "67"
Method Regexp#named_captures
returns a hash of the capture names and substrings; method Regexp#names
returns an array of the capture names.
A group may be made atomic with (?>
subexpression)
.
This causes the subexpression to be matched independently of the rest of the expression, so that the matched substring becomes fixed for the remainder of the match, unless the entire subexpression must be abandoned and subsequently revisited.
In this way subexpression is treated as a non-divisible whole. Atomic grouping is typically used to optimise patterns to prevent needless backtracking .
Example (without atomic grouping):
/".*"/.match('"Quote"') # => #<MatchData "\"Quote\"">
Analysis:
The leading subexpression "
in the pattern matches the first character "
in the target string.
The next subexpression .*
matches the next substring Quote“
(including the trailing double-quote).
Now there is nothing left in the target string to match the trailing subexpression "
in the pattern; this would cause the overall match to fail.
The matched substring is backtracked by one position: Quote
.
The final subexpression "
now matches the final substring "
, and the overall match succeeds.
If subexpression .*
is grouped atomically, the backtracking is disabled, and the overall match fails:
/"(?>.*)"/.match('"Quote"') # => nil
Atomic grouping can affect performance; see Atomic Group.
As seen above, a backreference number (\n
) or name (\k<name>
) gives access to a captured substring; the corresponding regexp subexpression may also be accessed, via the number (\gn
) or name (\g<name>
):
/\A(?<paren>\(\g<paren>*\))*\z/.match('(())') # ^1 # ^2 # ^3 # ^4 # ^5 # ^6 # ^7 # ^8 # ^9 # ^10
The pattern:
Matches at the beginning of the string, i.e. before the first character.
Enters a named group paren
.
Matches the first character in the string, '('
.
Calls the paren
group again, i.e. recurses back to the second step.
Re-enters the paren
group.
Matches the second character in the string, '('
.
Attempts to call paren
a third time, but fails because doing so would prevent an overall successful match.
Matches the third character in the string, ')'
; marks the end of the second recursive call
Matches the fourth character in the string, ')'
.
Matches the end of the string.
See Subexpression calls.
The conditional construct takes the form (?(cond)yes|no)
, where:
cond may be a capture number or name.
The match to be applied is yes if cond is captured; otherwise the match to be applied is no.
If not needed, |no
may be omitted.
Examples:
re = /\A(foo)?(?(1)(T)|(F))\z/ re.match('fooT') # => #<MatchData "fooT" 1:"foo" 2:"T" 3:nil> re.match('F') # => #<MatchData "F" 1:nil 2:nil 3:"F"> re.match('fooF') # => nil re.match('T') # => nil re = /\A(?<xyzzy>foo)?(?(<xyzzy>)(T)|(F))\z/ re.match('fooT') # => #<MatchData "fooT" xyzzy:"foo"> re.match('F') # => #<MatchData "F" xyzzy:nil> re.match('fooF') # => nil re.match('T') # => nil
The absence operator is a special group that matches anything which does not match the contained subexpressions.
/(?~real)/.match('surrealist') # => #<MatchData "surrea"> /(?~real)ist/.match('surrealist') # => #<MatchData "ealist"> /sur(?~real)ist/.match('surrealist') # => nil
The /\p{property_name}/
construct (with lowercase p
) matches characters using a Unicode property name, much like a character class; property Alpha
specifies alphabetic characters:
/\p{Alpha}/.match('a') # => #<MatchData "a"> /\p{Alpha}/.match('1') # => nil
A property can be inverted by prefixing the name with a caret character (^
):
/\p{^Alpha}/.match('1') # => #<MatchData "1"> /\p{^Alpha}/.match('a') # => nil
Or by using \P
(uppercase P
):
/\P{Alpha}/.match('1') # => #<MatchData "1"> /\P{Alpha}/.match('a') # => nil
See Unicode Properties for regexps based on the numerous properties.
Some commonly-used properties correspond to POSIX bracket expressions:
/\p{Alnum}/
: Alphabetic and numeric character
/\p{Alpha}/
: Alphabetic character
/\p{Blank}/
: Space or tab
/\p{Cntrl}/
: Control character
/\p{Digit}/
: Digit characters, and similar)
/\p{Lower}/
: Lowercase alphabetical character
/\p{Print}/
: Like \p{Graph}
, but includes the space character
/\p{Punct}/
: Punctuation character
/\p{Space}/
: Whitespace character ([:blank:]
, newline, carriage return, etc.)
/\p{Upper}/
: Uppercase alphabetical
/\p{XDigit}/
: Digit allowed in a hexadecimal number (i.e., 0-9a-fA-F)
These are also commonly used:
/\p{Emoji}/
: Unicode emoji.
/\p{Graph}/
: Non-blank character (excludes spaces, control characters, and similar).
/\p{Word}/
: A member in one of these Unicode character categories (see below) or having one of these Unicode properties:
Unicode categories:
Mark
(M
).
Decimal Number
(Nd
)
Connector Punctuation
(Pc
).
Unicode properties:
Alpha
Join_Control
/\p{ASCII}/
: A character in the ASCII character set.
/\p{Any}/
: Any Unicode character (including unassigned characters).
/\p{Assigned}/
: An assigned character.
A Unicode character category name:
May be either its full name or its abbreviated name.
Is case-insensitive.
Treats a space, a hyphen, and an underscore as equivalent.
Examples:
/\p{lu}/ # => /\p{lu}/ /\p{LU}/ # => /\p{LU}/ /\p{Uppercase Letter}/ # => /\p{Uppercase Letter}/ /\p{Uppercase_Letter}/ # => /\p{Uppercase_Letter}/ /\p{UPPERCASE-LETTER}/ # => /\p{UPPERCASE-LETTER}/
Below are the Unicode character category abbreviations and names. Enumerations of characters in each category are at the links.
Letters:
L
, Letter
: LC
, Lm
, or Lo
.
LC
, Cased_Letter
: Ll
, Lt
, or Lu
.
Marks:
M
, Mark
: Mc
, Me
, or Mn
.
Numbers:
N
, Number
: Nd
, Nl
, or No
.
Punctation:
P
, Punctuation
: Pc
, Pd
, Pe
, Pf
, Pi
, Po
, or Ps
.
S
, Symbol
: Sc
, Sk
, Sm
, or So
.
Z
, Separator
: Zl
, Zp
, or Zs
.
C
, Other
: Cc
, Cf
, Cn
, Co
, or Cs
.
Among the Unicode properties are:
A POSIX bracket expression is also similar to a character class. These expressions provide a portable alternative to the above, with the added benefit of encompassing non-ASCII characters:
/\d/
matches only ASCII decimal digits 0
through 9
.
/[[:digit:]]/
matches any character in the Unicode Decimal Number
(Nd
) category; see below.
The POSIX bracket expressions:
/[[:digit:]]/
: Matches a Unicode digit:
/[[:digit:]]/.match('9') # => #<MatchData "9"> /[[:digit:]]/.match("\u1fbf9") # => #<MatchData "9">
/[[:xdigit:]]/
: Matches a digit allowed in a hexadecimal number; equivalent to [0-9a-fA-F]
.
/[[:upper:]]/
: Matches a Unicode uppercase letter:
/[[:upper:]]/.match('A') # => #<MatchData "A"> /[[:upper:]]/.match("\u00c6") # => #<MatchData "Æ">
/[[:lower:]]/
: Matches a Unicode lowercase letter:
/[[:lower:]]/.match('a') # => #<MatchData "a"> /[[:lower:]]/.match("\u01fd") # => #<MatchData "ǽ">
/[[:alpha:]]/
: Matches /[[:upper:]]/
or /[[:lower:]]/
.
/[[:alnum:]]/
: Matches /[[:alpha:]]/
or /[[:digit:]]/
.
/[[:space:]]/
: Matches Unicode space character:
/[[:space:]]/.match(' ') # => #<MatchData " "> /[[:space:]]/.match("\u2005") # => #<MatchData " ">
/[[:blank:]]/
: Matches /[[:space:]]/
or tab character:
/[[:blank:]]/.match(' ') # => #<MatchData " "> /[[:blank:]]/.match("\u2005") # => #<MatchData " "> /[[:blank:]]/.match("\t") # => #<MatchData "\t">
/[[:cntrl:]]/
: Matches Unicode control character:
/[[:cntrl:]]/.match("\u0000") # => #<MatchData "\u0000"> /[[:cntrl:]]/.match("\u009f") # => #<MatchData "\u009F">
/[[:graph:]]/
: Matches any character except /[[:space:]]/
or /[[:cntrl:]]/
.
/[[:print:]]/
: Matches /[[:graph:]]/
or space character.
/[[:punct:]]/
: Matches any (Unicode punctuation character}[www.compart.com/en/unicode/category/Po]:
Ruby also supports these (non-POSIX) bracket expressions:
/[[:ascii:]]/
: Matches a character in the ASCII character set.
/[[:word:]]/
: Matches a character in one of these Unicode character categories or having one of these Unicode properties:
Unicode categories:
Mark
(M
).
Decimal Number
(Nd
)
Connector Punctuation
(Pc
).
Unicode properties:
Alpha
Join_Control
A comment may be included in a regexp pattern using the (?#
comment)
construct, where comment is a substring that is to be ignored. arbitrary text ignored by the regexp engine:
/foo(?#Ignore me)bar/.match('foobar') # => #<MatchData "foobar">
The comment may not include an unescaped terminator character.
See also Extended Mode.
Each of these modifiers sets a mode for the regexp:
i
: /pattern/i
sets Case-Insensitive Mode.
m
: /pattern/m
sets Multiline Mode.
x
: /pattern/x
sets Extended Mode.
o
: /pattern/o
sets Interpolation Mode.
Any, all, or none of these may be applied.
Modifiers i
, m
, and x
may be applied to subexpressions:
(?modifier)
turns the mode “on” for ensuing subexpressions
(?-modifier)
turns the mode “off” for ensuing subexpressions
(?modifier:subexp)
turns the mode “on” for subexp within the group
(?-modifier:subexp)
turns the mode “off” for subexp within the group
Example:
re = /(?i)te(?-i)st/ re.match('test') # => #<MatchData "test"> re.match('TEst') # => #<MatchData "TEst"> re.match('TEST') # => nil re.match('teST') # => nil re = /t(?i:e)st/ re.match('test') # => #<MatchData "test"> re.match('tEst') # => #<MatchData "tEst"> re.match('tEST') # => nil
Method Regexp#options
returns an integer whose value showing the settings for case-insensitivity mode, multiline mode, and extended mode.
By default, a regexp is case-sensitive:
/foo/.match('FOO') # => nil
Modifier i
enables case-insensitive mode:
/foo/i.match('FOO') # => #<MatchData "FOO">
Method Regexp#casefold?
returns whether the mode is case-insensitive.
The multiline-mode in Ruby is what is commonly called a “dot-all mode”:
Without the m
modifier, the subexpression .
does not match newlines:
/a.c/.match("a\nc") # => nil
With the modifier, it does match:
/a.c/m.match("a\nc") # => #<MatchData "a\nc">
Unlike other languages, the modifier m
does not affect the anchors ^
and $
. These anchors always match at line-boundaries in Ruby.
Modifier x
enables extended mode, which means that:
Literal white space in the pattern is to be ignored.
Character #
marks the remainder of its containing line as a comment, which is also to be ignored for matching purposes.
In extended mode, whitespace and comments may be used to form a self-documented regexp.
Regexp
not in extended mode (matches some Roman numerals):
pattern = '^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$' re = /#{pattern}/ re.match('MCMXLIII') # => #<MatchData "MCMXLIII" 1:"CM" 2:"XL" 3:"III">
Regexp
in extended mode:
pattern = <<-EOT ^ # beginning of string M{0,3} # thousands - 0 to 3 Ms (CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 Cs), # or 500-800 (D, followed by 0 to 3 Cs) (XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 Xs), # or 50-80 (L, followed by 0 to 3 Xs) (IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 Is), # or 5-8 (V, followed by 0 to 3 Is) $ # end of string EOT re = /#{pattern}/x re.match('MCMXLIII') # => #<MatchData "MCMXLIII" 1:"CM" 2:"XL" 3:"III">
Modifier o
means that the first time a literal regexp with interpolations is encountered, the generated Regexp
object is saved and used for all future evaluations of that literal regexp. Without modifier o
, the generated Regexp
is not saved, so each evaluation of the literal regexp generates a new Regexp
object.
Without modifier o
:
def letters; sleep 5; /[A-Z][a-z]/; end words = %w[abc def xyz] start = Time.now words.each {|word| word.match(/\A[#{letters}]+\z/) } Time.now - start # => 15.0174892
With modifier o
:
start = Time.now words.each {|word| word.match(/\A[#{letters}]+\z/o) } Time.now - start # => 5.0010866
Note that if the literal regexp does not have interpolations, the o
behavior is the default.
By default, a regexp with only US-ASCII characters has US-ASCII encoding:
re = /foo/ re.source.encoding # => #<Encoding:US-ASCII> re.encoding # => #<Encoding:US-ASCII>
A regular expression containing non-US-ASCII characters is assumed to use the source encoding. This can be overridden with one of the following modifiers.
/pat/n
: US-ASCII if only containing US-ASCII characters, otherwise ASCII-8BIT:
/foo/n.encoding # => #<Encoding:US-ASCII> /foo\xff/n.encoding # => #<Encoding:ASCII-8BIT> /foo\x7f/n.encoding # => #<Encoding:US-ASCII>
/pat/u
: UTF-8
/foo/u.encoding # => #<Encoding:UTF-8>
/pat/e
: EUC-JP
/foo/e.encoding # => #<Encoding:EUC-JP>
/pat/s
: Windows-31J
/foo/s.encoding # => #<Encoding:Windows-31J>
A regexp can be matched against a target string when either:
They have the same encoding.
The regexp’s encoding is a fixed encoding and the string contains only ASCII characters. Method
Regexp#fixed_encoding?
returns whether the regexp has a fixed encoding.
If a match between incompatible encodings is attempted an Encoding::CompatibilityError
exception is raised.
Example:
re = eval("# encoding: ISO-8859-1\n/foo\\xff?/") re.encoding # => #<Encoding:ISO-8859-1> re =~ "foo".encode("UTF-8") # => 0 re =~ "foo\u0100" # Raises Encoding::CompatibilityError
The encoding may be explicitly fixed by including Regexp::FIXEDENCODING
in the second argument for Regexp.new
:
# Regexp with encoding ISO-8859-1. re = Regexp.new("a".force_encoding('iso-8859-1'), Regexp::FIXEDENCODING) re.encoding # => #<Encoding:ISO-8859-1> # Target string with encoding UTF-8. s = "a\u3042" s.encoding # => #<Encoding:UTF-8> re.match(s) # Raises Encoding::CompatibilityError.
When either a regexp source or a target string comes from untrusted input, malicious values could become a denial-of-service attack; to prevent such an attack, it is wise to set a timeout.
Regexp has two timeout values:
A class default timeout, used for a regexp whose instance timeout is nil
; this default is initially nil
, and may be set by method Regexp.timeout=
:
Regexp.timeout # => nil Regexp.timeout = 3.0 Regexp.timeout # => 3.0
An instance timeout, which defaults to nil
and may be set in Regexp.new
:
re = Regexp.new('foo', timeout: 5.0) re.timeout # => 5.0
When regexp.timeout is nil
, the timeout “falls through” to Regexp.timeout
; when regexp.timeout is non-nil
, that value controls timing out:
| regexp.timeout Value | Regexp.timeout Value | Result | |----------------------|----------------------|-----------------------------| | nil | nil | Never times out. | | nil | Float | Times out in Float seconds. | | Float | Any | Times out in Float seconds. |
For certain values of the pattern and target string, matching time can grow polynomially or exponentially in relation to the input size; the potential vulnerability arising from this is the regular expression denial-of-service (ReDoS) attack.
Regexp matching can apply an optimization to prevent ReDoS attacks. When the optimization is applied, matching time increases linearly (not polynomially or exponentially) in relation to the input size, and a ReDoS attach is not possible.
This optimization is applied if the pattern meets these criteria:
No backreferences.
No subexpression calls.
No nested lookaround anchors or atomic groups.
No nested quantifiers with counting (i.e. no nested {n}
, {min,}
, {,max}
, or {min,max}
style quantifiers)
You can use method Regexp.linear_time?
to determine whether a pattern meets these criteria:
Regexp.linear_time?(/a*/) # => true Regexp.linear_time?('a*') # => true Regexp.linear_time?(/(a*)\1/) # => false
However, an untrusted source may not be safe even if the method returns true
, because the optimization uses memoization (which may invoke large memory consumption).
Read (online PDF books):
Mastering Regular Expressions by Jeffrey E.F. Friedl.
Regular Expressions Cookbook by Jan Goyvaerts & Steven Levithan.
Explore, test (interactive online editor):
Ripper
is a Ruby script parser.
You can get information from the parser with event-based style. Information such as abstract syntax trees or simple lexical analysis of the Ruby program.
Ripper
provides an easy interface for parsing your program into a symbolic expression tree (or S-expression).
Understanding the output of the parser may come as a challenge, it’s recommended you use PP
to format the output for legibility.
require 'ripper' require 'pp' pp Ripper.sexp('def hello(world) "Hello, #{world}!"; end') #=> [:program, [[:def, [:@ident, "hello", [1, 4]], [:paren, [:params, [[:@ident, "world", [1, 10]]], nil, nil, nil, nil, nil, nil]], [:bodystmt, [[:string_literal, [:string_content, [:@tstring_content, "Hello, ", [1, 18]], [:string_embexpr, [[:var_ref, [:@ident, "world", [1, 27]]]]], [:@tstring_content, "!", [1, 33]]]]], nil, nil, nil]]]]
You can see in the example above, the expression starts with :program
.
From here, a method definition at :def
, followed by the method’s identifier :@ident
. After the method’s identifier comes the parentheses :paren
and the method parameters under :params
.
Next is the method body, starting at :bodystmt
(stmt
meaning statement), which contains the full definition of the method.
In our case, we’re simply returning a String
, so next we have the :string_literal
expression.
Within our :string_literal
you’ll notice two @tstring_content
, this is the literal part for Hello,
and !
. Between the two @tstring_content
statements is a :string_embexpr
, where embexpr is an embedded expression. Our expression consists of a local variable, or var_ref
, with the identifier (@ident
) of world
.
ruby 1.9 (support CVS HEAD only)
bison 1.28 or later (Other yaccs do not work)
Ruby License.
Minero Aoki
aamine@loveruby.net