ruby-changes:65238
From: Nobuyoshi <ko1@a...>
Date: Thu, 11 Feb 2021 22:23:37 +0900 (JST)
Subject: [ruby-changes:65238] f3f78f9654 (master): [DOC] Improved regexp.rdoc [ci skip]
https://git.ruby-lang.org/ruby.git/commit/?id=f3f78f9654 From f3f78f96548e10e76784b2b45771b41dd358afd2 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Thu, 11 Feb 2021 22:20:41 +0900 Subject: [DOC] Improved regexp.rdoc [ci skip] * Sub-sectioned "Repetition" section * Added examples of "Possessive match" --- doc/regexp.rdoc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/doc/regexp.rdoc b/doc/regexp.rdoc index d84cae1..e25f10f 100644 --- a/doc/regexp.rdoc +++ b/doc/regexp.rdoc @@ -190,6 +190,8 @@ At least one uppercase character ('H'), at least one lowercase character https://github.com/ruby/ruby/blob/trunk/doc/regexp.rdoc#L190 "Hello".match(/[[:upper:]]+[[:lower:]]+l{2}o/) #=> #<MatchData "Hello"> +=== Greedy match + Repetition is <i>greedy</i> by default: as many occurrences as possible are matched while still allowing the overall match to succeed. By contrast, <i>lazy</i> matching makes the minimal amount of matches @@ -206,11 +208,17 @@ Both patterns below match the string. The first uses a greedy quantifier so https://github.com/ruby/ruby/blob/trunk/doc/regexp.rdoc#L208 /<.+>/.match("<a><b>") #=> #<MatchData "<a><b>"> /<.+?>/.match("<a><b>") #=> #<MatchData "<a>"> +=== Possessive match + A quantifier followed by <tt>+</tt> matches <i>possessively</i>: once it has matched it does not backtrack. They behave like greedy quantifiers, but having matched they refuse to "give up" their match even if this jeopardises the overall match. + /<.*><.+>/.match("<a><b>") #=> #<MatchData "<a><b>"> + /<.*+><.+>/.match("<a><b>") #=> nil + /<.*><.++>/.match("<a><b>") #=> nil + == Capturing Parentheses can be used for <i>capturing</i>. The text enclosed by the @@ -230,6 +238,8 @@ available with its #[] method: https://github.com/ruby/ruby/blob/trunk/doc/regexp.rdoc#L238 /[csh](..) [csh]\1 in/.match("The cat sat in the hat")[1] #=> 'at' +=== Named captures + Capture groups can be referred to by name when defined with the <tt>(?<</tt><i>name</i><tt>>)</tt> or <tt>(?'</tt><i>name</i><tt>')</tt> constructs. -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/