in reply to Quantifier follows nothing error in Regexp::Grammars

The question mark is a quantifier

$ perl -le " qr{\d****} " Nested quantifiers in regex; marked by <-- HERE in m/\d** <-- HERE **/ + at -e line 1. $ perl -le " qr{(?#an error follows)****} " Quantifier follows nothing in regex; marked by <-- HERE in m/(?#an err +or follows)* <-- HERE ***/ at -e line 1. $ perl -le " qr{?} " Quantifier follows nothing in regex; marked by <-- HERE in m/? <-- HER +E / at -e line 1.

You need to learn to character class :) See perlrecharclass

Try  <token: punctuation>  ( [.,?!"';:\-&()\[\]]+ )

See also RFC: A walkthrough from JSON ABNF to Regexp::Grammars and The usage of <ALIAS=\IDENT> of Regexp::Grammars

Replies are listed 'Best First'.
Re^2: Quantifier follows nothing error in Regexp::Grammars (quantifier "?")
by hdb (Monsignor) on Oct 09, 2013 at 07:14 UTC

    Minor comment: the hyphen should be the first character in the character class:

    <token: punctuation> ( [-.,?!"';:\&()\[\]]+ )

    otherwise it is interpreted as defining a range rather than a literal hypen.

      the hyphen should be the first character in the character class ... otherwise it is interpreted as defining a range rather than a literal hypen.

      ...or the last...
      ...or escaped with a backslash \.

      Cheers, Sören

      Créateur des bugs mobiles - let loose once, run everywhere.
      (hooked on the Perl Programming language)

      perldoc perlrecharclass says you don't need to escape [ within a (non-Posix) character class, and for ] there is a similiar rule as for the hyphen, so
      <token: punctuation> ( [][.,?!"';:&()-]+ )
      should be valid without any backslash.
      Update: corrected position of hyphen, had it outside of the regex :-/

      Minor comment: the hyphen should be the first character in the character class:

      Why do you think so, what are the reasons for this habit/style?

      I vaguely recall maybe that I read this advice before, but it didn't stick :) (not memorable)

        If you write /[:-&]/, you get Invalid [] range ":-&" in regex; marked by <-- HERE in m/[:-& <-- HERE ]/ because ':' comes later in the ASCII table than '&'.

        If you write /[&-:]/, it matches each of &'()*+,-./0123456789:, the full range in the ASCII table between ':' and '&'.

        If you write /[-&:]/, it matches only one of -&: as the hyphen is not interpreted as a range operator.

        Of course, you can escape it with a backslash as well, which is the case in the post above but I had not seen when I first looked at it.

        what are the reasons for this habit/style?

        in a character class, the hyphen is a range operator as in [0-9A-Za-z] or [A-z]-foolery.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)