On think Geek, they have this on a shirt:
/(bb|[^b]{2})/
But that's "To be or not be twice".

Surely they meant this:

/(bb|(?:[^b].|.[^b]))/
And if they did, that can be shortened to:
/(.{2})/
So, my assertion is that this is the *really* geeky way of saying it :)

Thoughts?

cLive ;-)

Replies are listed 'Best First'.
Re: to be or not to be...
by Jenda (Abbot) on Apr 08, 2003 at 12:08 UTC

    I think the code should be:

    /(bb|(?!bb)..)/

    Update:And I think the /(.{2})/ should be read "give me anything, but I want it twice!".

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

Re: to be or not to be...
by crenz (Priest) on Apr 08, 2003 at 11:22 UTC

    But that's "To be or not be twice".

    Right, I'd interpret that their specification was "Two 'b' or not two 'b'". In that case their code is wrong. For example, "ab" is not two 'b's, which means the second boolean condition evals to true (ie., it meets the specification). However, it does not match /(bb|[^b]{2})/, so the code is wrong.

    And if they did, that can be shortened to: /(.{2})/

    No, because that is highly political code. It says something without expressing any meaning, and is completely interchangeable for a lot of situations.

Re: to be or not to be...
by thelenm (Vicar) on Apr 08, 2003 at 17:47 UTC
    Since the logical expression "to be or not to be" (two b's or not two b's) will always evaluate to be true given any input (even strings longer or shorter than 2 characters), you can shorten your code even further with this expression:
    1
    (or any other true value you like). :-)

    -- Mike

    --
    just,my${.02}

Re: to be or not to be...
by dragonchild (Archbishop) on Apr 08, 2003 at 14:44 UTC
    Wouldn't /^(?:bb)|(?!bb)$/ work better?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Well, not. That would mean "to be at the beginning or not to be at the end". And the /(?!bb)$ would match anything. If you are at the very end of the string you may be sure there are not two more "b"s left. :-)

      Jenda
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
         -- Rick Osborne

      Edit by castaway: Closed small tag in signature

Re: to be or not to be...
by Rich36 (Chaplain) on Apr 08, 2003 at 15:23 UTC

    Or you could do it conditionally. Prints either 2 B's or something that's not 2 B's.

    #!/usr/local/bin/perl -l foreach ('b', 'bb', 'B', 'foo', 'bar', 'BB') { print m/^b{2}$/i ? "\x42\x42" : bb(); } sub bb() { while(1) { return chr(rand(100)) . chr(rand(100)) unless (m/^b{2}$ +/i); } }

    «Rich36»