From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-4.1 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id C644D1F453 for ; Sat, 16 Feb 2019 02:21:25 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id A0CEB121241; Sat, 16 Feb 2019 11:21:21 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id CFE1B121CA8 for ; Sat, 16 Feb 2019 11:21:18 +0900 (JST) Received: by filter0060p3iad2.sendgrid.net with SMTP id filter0060p3iad2-9363-5C67739C-8 2019-02-16 02:21:16.275725958 +0000 UTC m=+1586.574706229 Received: from herokuapp.com (ec2-54-221-130-143.compute-1.amazonaws.com [54.221.130.143]) by ismtpd0002p1iad1.sendgrid.net (SG) with ESMTP id vGGRBuIASBSELQP7uU8-HQ for ; Sat, 16 Feb 2019 02:21:16.197 +0000 (UTC) Date: Sat, 16 Feb 2019 02:21:17 +0000 (UTC) From: davisjam@vt.edu To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67024 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15583 X-Redmine-Issue-Author: davisjam X-Redmine-Sender: davisjam X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS63FupWqOFKjRaofDPQH9bo7oswXbtsuvkSsb 6WIAtPj9m9FhAimPWQtfvQI2fNDM7e5DOMjCl/gL6PaNmbB57shaqDOt4Rn1bxkCRVqpdAEIYCMqCs /OoONbMP4aQtJTaJOJ36u/W8FkdgWM6tzeeQ X-ML-Name: ruby-core X-Mail-Count: 91572 Subject: [ruby-core:91572] [Ruby trunk Bug#15583] Regex: ? on quantified group {n} is interpreted as optional, should be lazy X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #15583 has been updated by davisjam (James Davis). Can we change the documentation? I am happy to propose additional text. ---------------------------------------- Bug #15583: Regex: ? on quantified group {n} is interpreted as optional, should be lazy https://bugs.ruby-lang.org/issues/15583#change-76836 * Author: davisjam (James Davis) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: 2.6.1 * Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- The Ruby regex docs have this to say about repetition ([specific link](https://ruby-doc.org/core-2.6.1/Regexp.html#class-Regexp-label-Repetition)): > The constructs described so far match a single character. They can be followed by a repetition metacharacter to specify how many times they need to occur. Such metacharacters are called quantifiers. > > - * - Zero or more times > - ... > - {n} - Exactly n times > - ... >From this I conclude that the {n} construct is considered a quantifier metacharacter. The docs go on to say > 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 necessary for overall success. A greedy metacharacter can be made lazy by following it with ?. Since `{n}` is a greedy metacharacter, it seems like `{n}?` should make it lazy. In the particular case of `{n}?`, laziness is meaningless -- the regex engine must match n of whatever is being quantified, lazily or not. But I think other behavior is needlessly confusing. To make `{n}` optional, I think I should have to wrap it in parentheses: `(a{n})?`. The docs make it sound like `?` as "lazy" has stronger precedence than `?` as "optional". This make sense to me -- the "optional" meaning can be communicated using parentheses while the lazy meaning cannot. Here is a test program to explore this behavior: ```ruby if /a{1,}?/.match("") puts "a{1,}? matched the empty string" else puts "a{1,}? did not match" end if /a{1,3}?/.match("") puts "a{1,3}? matched the empty string" else puts "a{1,3}? did not match" end if /a{,1}?/.match("") puts "a{,1}? matched the empty string" else puts "a{,1}? did not match" end if /a{1}?/.match("") puts "a{1}? matched the empty string" else puts "Did not match" end ``` If `?` attaches more strongly to quantifers (to mean non-greedy) than to arbitrary patterns (to mean optional), then I expect it to mean "non-greedy" in each of these cases. So the expected behavior is: 1. `/a{1,}?/` *should not* match the empty string, since even non-greedily it must match at least 1 a. 2. `/a{1,3}?/` *should not* match the empty string, since even non-greedily it must match at least 1 a. 3. `/a{,1}?/` *should* match the empty string, since non-greedily it can match 0 a's. 4. `/a{1}?/` *should not* match the empty string, since even non-greedily it must match at least 1 a. Let's see how it behaves in Ruby 2.6.1: ```shell (09:43:09) jamie@woody /tmp $ ruby -v ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-linux] (09:43:12) jamie@woody /tmp $ ruby /tmp/t.rb a{1,}? did not match a{1,3}? did not match a{,1}? matched the empty string a{1}? matched the empty string ``` Cases 1-3 all behave as expected. However, case 4 matches the empty string, implying that in `/a{1}?/` the `?` interpreted to mean optional rather than non-greedy. I find this inconsistency a bit confusing. I tested this behavior in 7 other languages: Go, Java, JavaScript, Perl, PHP, Python, and Rust. In those languages, /a{1}?/ does not match the empty string (and is thus the `{n}?` notation interpreted as non-greedy rather than optional). Perhaps this should be addressed via a docs change to avoid possible breakage. Here is some possible wording: 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 necessary for overall success. Most greedy metacharacters can be made lazy by following them with ?. For the {n} metacharacter, greedy and non-greedy behavior is identical and the ? instead makes the repeated pattern optional. ---Files-------------------------------- t.rb (397 Bytes) -- https://bugs.ruby-lang.org/