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

One might not hand-craft a regexp with single-entry character classes, but how about code-built regexps? When a regexp is built programatically, and there's a situation where one time a character class may contain one entry, and another times "n" entries, the fact that one-entry character classes are legal is simply one less thing to worry about. In that regard, I can see why it may be useful for such a construct to exist and to be legal.

And actually, there may be some single-entry character classes that are helpful, particularly among negated classes:

/[^T]ap/ # Match anything that's not 'T', followed by 'ap'

This is one time where a single-entry character class is actually probably the best way to do it.


Dave

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

Replies are listed 'Best First'.
Re^2: Why would one want in a regex a class with only a single entry?
by parv (Parson) on Mar 25, 2008 at 06:31 UTC
    If a negated class can qualify as a single character class, then so should the POSIX ones ([::]), property ones (\p{}), "normal" Perl backslash ones (\d, \s, etc.), among others.

      That doesn't bother me. The OP seemed to be discussing the validity and practicality of single-entry character classes. And I believe he was discussing them in the context of the [....] operator in a regexp. The backslash CC's and property CC's are already 'single entry', so probably not really so relevant to the discussion, particularly where they can stand-alone outside of a user-defined character class.

      Certainly the POSIX classes constitute "single entries". And that's definitely another good argument for why single-entry character classes are practical, and should be legal (which, of course, they are).

      The negated character class of a single entry just happened to be the first example that jumped to my mind. Your POSIX thought is great too.


      Dave

        Thanks for your response, as I was trying to clarify, to self, just what makes a "single entry class". I was thinking strictly of a character class without any meta characters.

        davido and pary, thanks. davido you're correct...I was checking not so much on the validity of a single entry class (I know it's legal). I was just curious about why a programmer might want to have a single character class. pary made me think and realize, much like you're note of the negated class, that there are meta-character situations where a single entry could be usefule, too.

        Thanks to you both. I appreciate your insights.

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

    Thanks, davido. You and ysth both hit on similar situations. I especially like the negated class (which ends up, in the regex engine being a many-valued class (I suspect...though I'm by no means that knowledgable of the regex engine).

    In my response to ysth, I wondered why one wouldn't except the single engtry, programmatically built-up case and provide a more regex-engine-efficient cast in that instance. Your comment that:

    ...is simply one less thing to worry about.

    makes good sense to me and is in the spirit of what a lot of Perl culture is all about.

    I think I'd probably take the extra effort to check for the single entry and try to cast it as a special case; but that's just my compulsive nature.

    Thanks, davido (and everyone) for your insights. You're all helping me to be a better monk.

    ack Albuquerque, NM