regex golf

Started by taxed, January 13, 2014, 04:05:17 PM

Previous topic - Next topic

taxed

I thought this was pretty cool...

http://www.i-programmer.info/news/144-graphics-and-games/6828-regex-golf-xkcd-and-peter-norvig.html

regex means "regular expressions", which are used to search and match patterns in strings or text.  A maybe well known one is '*', which matches zero or more of any character.  '.' matches 1 or more of any character.  '^' means anchor to the beginning, and '$' means anchor to the end.  There are a bunch of them: http://www.rexegg.com/regex-quickstart.html

If we have a few lines of text, like:

A boy and his dog.
We went to the store.
Liberals are dumb.

The regex /b/ would match the first and last sentences, since a "b" is found.  However, the regex /^b/ would not match any, since none of the sentences start with a "b".  The regex /e.$/ would match the second sentence, since an "e", followed by 1 or more characters (in this case, coincidentally a ".") are at the end.  There isn't a "right" way to match for a pattern, but the shorter the better I think.  So, the concept of regex golf is there is a bunch of text, like a paragraph or list of sentences, or whatever, and the players will match for a specific pattern using the shortest possible regex WHILE excluding other text.

The example in this article: http://nbviewer.ipython.org/url/norvig.com/ipython/xkcd1313.ipynb

matches for all the last names of winning Presidents, and NOT their opponents with the regex:

QuoteI found that the hover text, "/bu|[rn]t|[coy]e|[mtg]a|j|iso|n[hl]|[ae]d|lev|sh|[lnd]i|[po]o|ls/ matches the last names of elected US presidents but not their opponents.", contains a confusing contradiction. There are several last names (like "Nixon") that denote both elected presidents and opponents. So no regular expression could both match and not match "Nixon". I could only assume that Randall meant for these names to be winners and not losers (and in fact he later confirmed that was the correct interpretation).

#PureBlood #TrumpWon