diff --git a/doc/regexp.rdoc b/doc/regexp.rdoc index d84cae1771..e25f10fc66 100644 --- a/doc/regexp.rdoc +++ b/doc/regexp.rdoc @@ -190,6 +190,8 @@ At least one uppercase character ('H'), at least one lowercase character "Hello".match(/[[:upper:]]+[[:lower:]]+l{2}o/) #=> # +=== Greedy match + Repetition is greedy by default: as many occurrences as possible are matched while still allowing the overall match to succeed. By contrast, lazy 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("") #=> #"> /<.+?>/.match("") #=> #"> +=== Possessive match + A quantifier followed by + matches possessively: 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("") #=> #"> + /<.*+><.+>/.match("") #=> nil + /<.*><.++>/.match("") #=> nil + == Capturing Parentheses can be used for capturing. 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' +=== Named captures + Capture groups can be referred to by name when defined with the (?<name>) or (?'name') constructs.