# Regex - Match half of the strings

Okay, the challenge is that you have to write a regex that matches half of all strings.

Some rules to clarify:

For ANY string length(>0), if I permute all strings(of that length), exactly half of them should be matched. Put more lightly, if I generate a random string of that length it should have a 50% chance of being matched. (this means that you cannot just match all even-length strings)

You may assume that strings contain only A-Z, a-z, and 0-9.

0 length strings are undefined, you can choose to either match them or not.

Shortest regex wins.

• /^[A-Z0-4]*$/ works, doesn't it? – Tim Seguine Jan 16 '14 at 20:53 • @TimSeguine No, if strings could have only uppercase, or only lower case it would. But since most strings have a combination of both, that misses most strings – Cruncher Jan 16 '14 at 20:55 • /^[\w0-4]/ works, doesn't it? – John Dvorak Jan 16 '14 at 20:56 • oh yeah sorry, it works for one character but gets worse and worse after that. If i remove the star and the$ though, it should work. It takes then half of all one character strings and all of the other strings that start with one of those characters. – Tim Seguine Jan 16 '14 at 20:56
• @JanDvorak I know, that is exactly my point. You are matching too many one character strings. There are 62, and you match 57 of them. It does it that way in PCRE at least IIRC. Is there a dialect in which it doesn't? If there is, then I concede defeat. – Tim Seguine Jan 16 '14 at 21:08

## 6

/^[5-Z]/


In ASCII's order, there's 0-9 then A-Z then a-z So Half of the string begin with 5-Z, the other half doesn't.

• Dang, my answer is a duplicate of this... – Justin Jan 16 '14 at 21:21
• But thanks for posting it, it made me realize that the good answer didn't necessarily include half of the letters and half of the numbers. any 31-char subset of A-Za-z0-9 is fine – xem Jan 16 '14 at 21:24
• @xem you are already making me regret changing my display name to my real name. :P – Tim Seguine Jan 16 '14 at 21:27
• Oh god it's a real name? I'm sorry Tim. No more jokes. God bless you. I hope one day you'll forgive me. ='( – xem Jan 16 '14 at 21:30
• @xem when I see you in hell ;) – Tim Seguine Jan 16 '14 at 21:32

# 6

/^[0-U]/


Ranges work for ascii values, so this matches half of the 62 combinations of first letters. Can match the empty string for one extra char:

/^[0-U]?/


For more confusion, this is also a solution:

/^[--U]/


Also this:

/^[-U]/


where the ascii 0 is right before the -.

• /^[-U]/ is this considered 5 chars? Or still 6, just that one isn't printable? – Cruncher Feb 27 '14 at 14:42
• @Cruncher One was unprintable. Otherwise, that would only match the characters - and U. – Justin Feb 27 '14 at 19:57

9 characters (not counting leaning toothpicks)

 /^[A-Z0-4]/


It will match half of all 1 character strings. Every string of length greater than one starts with a one character string, half of which will match. So half match in total.

• +1 for leaning toothpicks if I could (but that would be +2) – John Dvorak Jan 16 '14 at 21:07

## 24

You didn't ask for a max length.

So an infinity of strings can exist.

Half of infinity = infinity.

/^[A-Z0-4][A-Za-z0-9]*\$/

• By your logic, all that is needed is /^[A-Z0-4]/, but that is Tim's solution... – Justin Jan 16 '14 at 21:08