11
$\begingroup$

Robert loves regular expressions and ever since his friend Jean introduced him to Regex Golf, he spends most of his time trying to find the shortest and most elegant solution possible. Robert is also very well-organized and likes to keep an archive containing all his regular expressions along with the test cases accompanying them.

For example [^ad]+ is his neuest regex. It matches the titles from every book in the Lord of the Rings trilogy:

  • The Fellowship of the Ring
  • The Two Towers
  • The Return of the King

and none from the A Song of Ice and Fire series:

  • A Game of Thrones
  • A Clash of Kings
  • A Storm of Swords
  • A Feast for Crows
  • A Dance with Dragons
  • The Winds of Winter
  • A Dream of Spring

However, he just noticed that he is missing the test case for his very first (and admittedly messy) regular expression:

Au.+|B[eu].+[ma]|C.+|D.+|E.+|F.+|G.+|H.+|I[rt].+|L.[tx].+|Mal.+|N.+s|P.+|Ro.+|Sl.+|S.[ae].+|(Un.+)?

Robert being the perfectionist he is desperately wants to remember what this monstrosity was even supposed to match, can you help him?

$\endgroup$
  • 1
    $\begingroup$ The Lord of the Rings is actually 3 volumes with 2 books in each. The titles you have listed are the titles of the volumes. #pedantic $\endgroup$ – Ian MacDonald Jan 11 '17 at 19:13
  • $\begingroup$ @IanMacDonald I just checked on my bookshelf and it turns out you are right. Though to be fair, it is easy to be overlooked $\endgroup$ – Ria Jan 11 '17 at 19:20
7
$\begingroup$

I think this regular expression matches ...

... the members of the European Union in a list of all European countries.

Explanation:

The vertical bar denotes alternatives, so if any of the patterns between bars match, the whole expression matches:

Au.+ → Austria
B[eu].+[ma] → Belgium, Bulgaria
C.+ → Croatia, Cyprus, Czech Republic
D.+ → Denmark
E.+ → Estonia
F.+ → Finland, France
G.+ → Germany, Greece
H.+ → Hungary
I[rt].+ → Ireland, Italy
L.[tx].+ → Latvia, Lithuania, Luxembourg
Mal.+ → Malta
N.+s → Netherlands
P.+ → Poland, Portugal
Ro.+ → Romania
Sl.+ → Slovakia
S.[ae].+ → Spain, Sweden
(Un.+)? → United Kingdom

Simple patterns like E.+ mean there is no ambiguity. More complex patterns must select EU members over non-EU members, e.g. none of the S pattern matches Switzerland.

$\endgroup$
  • $\begingroup$ Australia and Canada are both included, though. $\endgroup$ – Ian MacDonald Jan 11 '17 at 19:14
  • $\begingroup$ Yes, but that's not a European country. $\endgroup$ – M Oehm Jan 11 '17 at 19:14
  • $\begingroup$ Define "European country". Both of those are members of the British commonwealth. ;) $\endgroup$ – Ian MacDonald Jan 11 '17 at 19:15
  • 5
    $\begingroup$ Aristotelian physics say that a country that is even its own bloody continent can't be European. :) $\endgroup$ – M Oehm Jan 11 '17 at 19:19
  • 1
    $\begingroup$ And the typo hint, neuest gives nEUest $\endgroup$ – Beastly Gerbil Jan 11 '17 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.