in reply to Oddness with regex quantifiers

If you want at most MAX repetitions, use {0,MAX}

The reason for the asymmetry between {MIN,} and {,MAX} is probably that there's a zero in perl, but Inf isn't generally supported.

If you wonder what {,MAX} matches, here's the answer:

$ perl -Mre=debug -ce ' /a{,5}/' Compiling REx "a{,5}" Final program: 1: EXACT <a{,5}> (4) 4: END (0) anchored "a{,5}" at 0 (checking anchored isall) minlen 5 -e syntax OK Freeing REx: "a{,5}" $ perl -wE 'say "yes" if "a{,4}" =~ /a{,4}/' yes # in contrast: $ perl -Mre=debug -ce ' /a{0,5}/' Compiling REx "a{0,5}" Final program: 1: CURLY {0,5} (5) 3: EXACT <a> (0) 5: END (0) minlen 0 -e syntax OK Freeing REx: "a{0,5}"

Replies are listed 'Best First'.
Re^2: Oddness with regex quantifiers
by JavaFan (Canon) on Nov 23, 2010 at 23:29 UTC
    The reason for the asymmetry between {MIN,} and {,MAX} is probably that there's a zero in perl, but Inf isn't generally supported.
    The issue was raised not so long ago on p5p, it even caught old farts off guard. I don't think anyone recalled the reason why it does what it does. And people expressed the wish it would have been done otherwise in the past. Some have suggested a warning, but IIRC, nothing happened. It's unlikely the actual meaning is going to change. The advantages of not having to type a 0 don't out-weight the negative impact of potentially breaking code. It's one of the many things that with the benefit of hindsight would have been done differently.
        The issue was raised not so long ago on p5p, it even caught old farts off guard.

      Yeah .. caught this old fart off guard too. Glad to hear I wasn't the only one surprised. :)

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re^2: Oddness with regex quantifiers
by talexb (Chancellor) on Nov 23, 2010 at 21:36 UTC
      The reason for the asymmetry between {MIN,} and {,MAX} is probably that there's a zero in perl, but Inf isn't generally supported.

    Hmm .. but isn't the zero implied by the null field?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      Hmm .. but isn't the zero implied by the null field?

      Not if you expect .{MIN,} to match at least MIN characters. .{MIN,0} doesn't make much sense to me.

      Hmm .. but isn't the zero implied by the null field?

      Not really, I don't think.
      If perl accepted {,X}, I'm pretty sure some people would assume that it stood for {1,X}, not {0,X}. After all, one to X matches makes more sense than zero to X matches in many situations.
      It's propably best to force people to make an explicit choice.

        I'm pretty sure some people would assume that it stood for {1,X}, not {0,X}.

        That says nothing without an idea of how common such an assumption would be made and an idea of the size of the negative impact making that assumption would entail.

        one to X matches makes more sense than zero to X matches in many situations.

        It makes sense in some situations, not necessarily many.

        It's propably best to force people to make an explicit choice.

        It's even better not to break backwards compatibility.

        Sorry, everyone, I guess I should have said 1, not 0.

        Alex / talexb / Toronto

        "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      $ perl -E'say "aa" =~ /a{1,}/' 1 $ perl -E'say "aa" =~ /a{1,0}/' Can't do {n,m} with n > m in regex; marked by <-- HERE in m/a{1,0} <-- + HERE / at -e line 1.