ruby-changes:71733
From: Burdette <ko1@a...>
Date: Fri, 15 Apr 2022 04:25:21 +0900 (JST)
Subject: [ruby-changes:71733] 01395d84ab (master): More details for regexp literals (#5800)
https://git.ruby-lang.org/ruby.git/commit/?id=01395d84ab From 01395d84aba82cf9f9f6ac53aeb3e6f142bd8d83 Mon Sep 17 00:00:00 2001 From: Burdette Lamar <BurdetteLamar@Y...> Date: Thu, 14 Apr 2022 14:25:06 -0500 Subject: More details for regexp literals (#5800) --- doc/syntax/literals.rdoc | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/doc/syntax/literals.rdoc b/doc/syntax/literals.rdoc index 32fe5110ce..821eac411c 100644 --- a/doc/syntax/literals.rdoc +++ b/doc/syntax/literals.rdoc @@ -381,15 +381,15 @@ on the methods you need to implement. https://github.com/ruby/ruby/blob/trunk/doc/syntax/literals.rdoc#L381 == \Regexp Literals -A regular expression is created using "/": +A regular expression may be created using leading and trailing +slash (<tt>'/'</tt>) characters: - /my regular expression/ + re = /foo/ # => /foo/ + re.class # => Regexp -The regular expression may be followed by flags which adjust the matching -behavior of the regular expression. The "i" flag makes the regular expression -case-insensitive: - - /my regular expression/i +The trailing slash may be followed by one or more _flag_ characters +that modify the behavior. +See {Regexp options}[rdoc-ref:Regexp@Options] for details. Interpolation may be used inside regular expressions along with escaped characters. Note that a regular expression may require additional escaped @@ -482,13 +482,23 @@ You can write a symbol with <tt>%s</tt>: https://github.com/ruby/ruby/blob/trunk/doc/syntax/literals.rdoc#L482 === <tt>%r</tt>: Regexp Literals -You can write a regular expression with <tt>%r</tt>: +You can write a regular expression with <tt>%r</tt>; +the character used as the leading and trailing delimiter +may be (almost) any character: + + %r/foo/ # => /foo/ + %r:name/value pair: # => /name\/value pair/ + +A few "symmetrical" character pairs may be used as delimiters: - r = %r[foo\sbar] # => /foo\sbar/ - 'foo bar'.match(r) # => #<MatchData "foo bar"> - r = %r[foo\sbar]i # => /foo\sbar/i - 'FOO BAR'.match(r) # => #<MatchData "FOO BAR"> + %r[foo] # => /foo/ + %r{foo} # => /foo/ + %r(foo) # => /foo/ + %r<foo> # => /foo/ +The trailing delimiter may be followed by one or more _flag_ characters +that modify the behavior. +See {Regexp options}[rdoc-ref:Regexp@Options] for details. === <tt>%x</tt>: Backtick Literals -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/