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

I'm writing a program that would take a line of text, match on a word that would end in 'a' and also capture up to the next 5 characters. Here is the program.

r/bin/perl use v5.12; use warnings; ## 9/26/19 ## Program uses named captures instead of $1 chomp(my $text = <STDIN>); while (<>) { # take one input line at a time chomp; if ( $text =~ m/((?<word1>\b\w*a\b))(?<word2>\s+++++))/) { print "Matched: |$`<$&>$'|\n"; # the special match vars print " 'word' contains '$+{word1} $+<word2>'\n"; } else { print "No match: |$_|\n"; }

and here is the errors that I'm getting

Nested quantifiers in regex; marked by <-- HERE in m/((?<word1>\b\w*a\ +b))(?<word2>\s+++ <-- HERE ++))/ at ./Chapter8_Exer5 line 12

TIA The catfish

Replies are listed 'Best First'.
Re: Trying out 2 captures
by Fletch (Bishop) on Sep 26, 2019 at 21:08 UTC

    Your regex is invalid. + is a quantifier that means "one or more of the prior thing"; having a quantifier directly on a quantifier doesn't make any sense in the regex mini-language. If you mean five word characters, say that: \w{5}.

    Edit: Derp, yes ++ is now a valid extra greedy quantifier; point still stands that (using parens for illustrative grouping only) (++)+ doesn't make any sense which is why the error message points just after the third +.

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

Re: Trying out 2 captures
by AnomalousMonk (Archbishop) on Sep 26, 2019 at 21:40 UTC

    Further to Fletch's reply:   Please see perlre, perlretut, and perlrequick and perhaps some of these articles. (Note: From Perl version 5.10 onward,  ++ is one of a set of valid regex metacharacter sequences  ?+ *+ ++ {n,m}+ denoting a "possessive" quantifier that does not "give up" anything it may have matched. (Update: But  +++++ is still invalid!))


    Give a man a fish:  <%-{-{-{-<