in reply to Re: Quantifier follows nothing error in Regexp::Grammars (quantifier "?")
in thread Quantifier follows nothing error in Regexp::Grammars

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.

  • Comment on Re^2: Quantifier follows nothing error in Regexp::Grammars (quantifier "?")
  • Download Code

Replies are listed 'Best First'.
Re^3: Quantifier follows nothing error in Regexp::Grammars (quantifier "?")
by Happy-the-monk (Canon) on Oct 09, 2013 at 07:18 UTC

    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)

Re^3: Quantifier follows nothing error in Regexp::Grammars (quantifier "?")
by soonix (Chancellor) on Oct 09, 2013 at 09:58 UTC
    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 :-/
Re^3: Quantifier follows nothing error in Regexp::Grammars (hyphen charclass)
by Anonymous Monk on Oct 09, 2013 at 08:30 UTC

    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.

        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.

        Oh, I see :) I thought you might have had some other reasoning ... that maybe excluded backslash ... :) hi guy, its me I am

      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)