From 01395d84aba82cf9f9f6ac53aeb3e6f142bd8d83 Mon Sep 17 00:00:00 2001 From: Burdette Lamar 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(-) (limited to 'doc') 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. == \Regexp Literals -A regular expression is created using "/": +A regular expression may be created using leading and trailing +slash ('/') 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 %s: === %r: Regexp Literals -You can write a regular expression with %r: +You can write a regular expression with %r; +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) # => # - r = %r[foo\sbar]i # => /foo\sbar/i - 'FOO BAR'.match(r) # => # + %r[foo] # => /foo/ + %r{foo} # => /foo/ + %r(foo) # => /foo/ + %r # => /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. === %x: Backtick Literals -- cgit v1.2.3