/(bb|[^b]{2})/

/(bb|[^b]{2})/

…That is the question. ThinkGeek is selling that on a t-shirt for the "regular expression junkie + lover of literature." Wearing that around would be a sure way to get me to notice you, especially if you have a nice rack or happen to be Angelina Jolie.

Note that the parentheses are unnecessary except in the case of Perl-folk wishing to avoid using the naughty $& variable for performance reasons. Also, the expression is buggy in that many alternatives to "bb" are not matched, so it would probably be better written as simply /bb|.*/s. For a JavaScript solution you could try var answer = /^bb$/.test(i);.

Alright, I'm done debugging the shirt now.

16 thoughts on “/(bb|[^b]{2})/”

  1. Also –

    I had an atomic grouping with your mom and some friends, but her lazy repetition was not impressing me. Then when I was done she became a bit of a possessive quantifier so I finished the job with $.

  2. Haha, it was a bit of a stretch. But I did come up with some more generalized geek talk ones:

    Last night I inserted my Dongle into your mom’s hub but I didn’t use my Trojan.

    I spent all morning servicing your mom’s asynchronous requests. On an unrelated note I heard some guy invoked the getPenis() method of your service and received a 404 status code – Not found.

    I think your mom’s cache is going to need some clearing because I just filled it.

  3. Your mom’s repository is so old that I don’t need to use version control.

    As for your regex dirty-talk, that’s just dreadful, but I’ve got nothing.

  4. /bb|.*/s and /(bb|[^b]{2})/ aren’t equivalent my friend =)

    Any single-character string is matched by yours but not the original. Also any two-character string containing only one b. In fact, yours will match anything including an empty string.

    It’s funny to see Azat came up with essentially the same answer I did =) Except one half of his alternation consumes while the other half doesn’t – which might be his intention, but it’s more likely you’d want both sides of the alternation to consume, or neither side to consume. That’s why I prefer /(?=bb)|(?!bb)/ (non-consuming) or /(?:(?=bb)|(?!bb))../.

    This last one can be reduced to /.{2}/, but that doesn’t read quite as well =)

  5. /bb|.*/s and /(bb|[^b]{2})/ aren’t equivalent my friend =)

    No kidding. πŸ™‚ That’s why I posted my implementation. Anything other than “bb” (including the empty string) is not “bb”. Therefore I read my regex as “two Bs or anything other than two Bs”, while I read the original as “two Bs or any two non-Bs, captured to group one”. I read your regexes here as “over-engineered, inefficient patterns that simply match any position or any two, non-line break characters.” πŸ˜› Mine can alternatively be read as “match either ‘bb’ or the first line of the subject string.”

  6. Late to the party but I was searching your blog for the answer to something… and well while y’all can fight all you want about the regex I’m going to take exception to:

    var answer =

    it should be:

    var question =

    πŸ˜›

Leave a Reply

Your email address will not be published. Required fields are marked *