1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[DOC] Improved regexp.rdoc [ci skip]

* Sub-sectioned "Repetition" section
* Added examples of "Possessive match"
This commit is contained in:
Nobuyoshi Nakada 2021-02-11 22:20:41 +09:00
parent 8544f51ef7
commit f3f78f9654
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6

View file

@ -190,6 +190,8 @@ At least one uppercase character ('H'), at least one lowercase character
"Hello".match(/[[:upper:]]+[[:lower:]]+l{2}o/) #=> #<MatchData "Hello"> "Hello".match(/[[:upper:]]+[[:lower:]]+l{2}o/) #=> #<MatchData "Hello">
=== Greedy match
Repetition is <i>greedy</i> by default: as many occurrences as possible Repetition is <i>greedy</i> by default: as many occurrences as possible
are matched while still allowing the overall match to succeed. By are matched while still allowing the overall match to succeed. By
contrast, <i>lazy</i> matching makes the minimal amount of matches 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
/<.+>/.match("<a><b>") #=> #<MatchData "<a><b>"> /<.+>/.match("<a><b>") #=> #<MatchData "<a><b>">
/<.+?>/.match("<a><b>") #=> #<MatchData "<a>"> /<.+?>/.match("<a><b>") #=> #<MatchData "<a>">
=== Possessive match
A quantifier followed by <tt>+</tt> matches <i>possessively</i>: once it A quantifier followed by <tt>+</tt> matches <i>possessively</i>: once it
has matched it does not backtrack. They behave like greedy quantifiers, has matched it does not backtrack. They behave like greedy quantifiers,
but having matched they refuse to "give up" their match even if this but having matched they refuse to "give up" their match even if this
jeopardises the overall match. jeopardises the overall match.
/<.*><.+>/.match("<a><b>") #=> #<MatchData "<a><b>">
/<.*+><.+>/.match("<a><b>") #=> nil
/<.*><.++>/.match("<a><b>") #=> nil
== Capturing == Capturing
Parentheses can be used for <i>capturing</i>. The text enclosed by the Parentheses can be used for <i>capturing</i>. The text enclosed by the
@ -230,6 +238,8 @@ available with its #[] method:
/[csh](..) [csh]\1 in/.match("The cat sat in the hat")[1] #=> 'at' /[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 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> <tt>(?<</tt><i>name</i><tt>>)</tt> or <tt>(?'</tt><i>name</i><tt>')</tt>
constructs. constructs.