Win has asked for the wisdom of the Perl Monks concerning the following question:

What is the maximum string length you can specify in a regex?

eg. Can I take the following much beyond 1000

$rule->name( qr/^.{250,1000}$/);

Replies are listed 'Best First'.
Re: Regex question
by moritz (Cardinal) on Aug 04, 2009 at 15:04 UTC
    To quote perlre
    The "*" quantifier is equivalent to "{0,}", the "+" quantifier to "{1,}", and the "?" quantifier to "{0,1}". n and m are limited to integral values less than a preset limit defined when perl is built. This is usually 32766 on the most common platforms. The actual limit can be seen in the error message generated by code such as this:
    $_ **= $_ , / {$_} / for 2 .. 42;

    Searching in perlre for "limit" and stepping to the third hit revealed that.

Re: Regex question
by Fletch (Bishop) on Aug 04, 2009 at 14:17 UTC

    What happened when you tried it? You've been told before that you won't endanger the space-time continuum trying trivial experiments yourself.

    Forget it Jake, it's Win.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Regex question
by BioLion (Curate) on Aug 04, 2009 at 14:16 UTC

    I don't know, but if all you want to do is make sure names are between 250 and 1000 chars then you are better off using length in a coderef :

    $rule->name( {return 1 if ( (length >= 250) && (length <= 1000) ) } );

    not tested!! Just an idea!

    Just a something something...
      Nice idea, however surely the regex parser in Perl would optimise to that anyway, wouldn't it?

      Update: Your method does however offer the advantage of allowing me to find file with a greater length. I'll just use the > 250 bit

        I don't know - I also don't know whether using .(dot) to match long strings like that would also be expensive, because of how many things could match such a 'loose' regex. But i am out of my depth here, i freely admit to not knowing enough about the inner workings of regexes...

        Just a something something...
      btw. The following works. Your bit of code wasn't quite there. ;)

      $rule->name(length >= 250);
        Can't call method "name" on an undefined value at - line 1.
Re: Regex question
by Marshall (Canon) on Aug 05, 2009 at 07:53 UTC
    Certainly even 1 million chars would not be a limit in a Perl regex. There is really no absolute limit, only performance issues.

    Update: ok, in some systems the max limit will be limited by 16 bit positive integer.

    Let's not get de-focused here, what is it that you are trying to do? .{250,1000} looks like a strange thing to do.

    Your regex says at least 250 chars AND something less than or equal to 1,000 chars on that line. Meaning that 249 chars won't match. 1001 chars on the line won't match. 651 characters will match. What is the point and the application?

    #!/usr/bin.perl -w use strict; my $test = "1234"; my @digits = ($test =~ m/^(.{4,5})$/); print "digits = @digits\n"; #prints digits = 1234 $test = "12345"; @digits = ($test =~ m/^(.{4,5})$/); print "digits = @digits\n"; #prints digits = 12345 $test = "123456"; @digits = ($test =~ m/^(.{4,5})$/); print "digits = @digits\n"; #prints digits =
    There is another thread running about the difference been "nothing" and "undef" for an @var. There is a difference and the above shows it. The issue in that thread is Why? Not that this it is true. In this last example, @digits is "nothing", not undefined, same as @digits=();

    The above will print..

    digits = 1234 digits = 12345 digits =