in reply to Why would one want in a regex a class with only a single entry?

One justification I have heard people make (not that I subscribe to the idea myself) is that one doesn't have to worry so much about meta-characters, in that nearly every single character loses its meta function in a character class.

Instead of having to write /a\+b/, one can write /a[+]b/. Thus, instead of having to worry about whether a punctuation character needs to be escaped or not, one can stick it in a character class and be done with it.

I think this is a false economy, but, if it makes you happy...

• another intruder with the mooring in the heart of the Perl

  • Comment on Re: Why would one want in a regex a class with only a single entry?

Replies are listed 'Best First'.
Re^2: Why would one want in a regex a class with only a single entry?
by davido (Cardinal) on Mar 26, 2008 at 05:32 UTC

    It's not a false economy if, for legibility reasons:

    m/a[ ]b/ # You wish to make it obvious that a space is needed. m/a[ ]{2,5}b/ # You wish to define a quantifier on a whitespace m/a[ ]+b/ # character that would otherwise be difficult to read. m/a[ ]b/x # You're using the /x modifier where whitespace # is ignored by default.

    Dave

Re^2: Why would one want in a regex a class with only a single entry?
by ack (Deacon) on Mar 26, 2008 at 04:27 UTC

    Thanks, grinder. I believe, that this is very similar to tsf's use for adding in white space when using the /x modifier.

    I would note that although, as you noted it may be:

    ...a false economy...

    it does seem to improve readability since, to me, /a[+]b/ seems more readable than /a\+b/.

    ack Albuquerque, NM