6
\$\begingroup\$

Wikipedia has several restrictions on an article's title. I summarize them here:

  • It may not be empty.
  • It may not begin with a lowercase letter. A character is considered lowercase if it has an Ll after its name in UnicodeData.txt.
  • It may not contain any of the characters: # < > [ ] | { } _
  • It may not be . or ..
  • It may not begin with ./ or ../.
  • It may not contain /./ or /../.
  • It may not end with /. or /...
  • It may not contain any space character (a character is considered a space if it has Zs after its name in UnicodeData.txt) except for U+0020 SPACE.
  • It may not contain more than one consecutive space.
  • It may not consist entirely of spaces.
  • It may not begin with a colon.
  • It may not begin with the language prefix of any of the 292 language editions of Wikipedia followed by a colon. This includes en: for English.
  • It may not begin with the prefix of another Wikimedia Foundation project or its shortcut followed by a colon.
  • It may not contain the character % followed by two hexadecimal digits.
  • It may not contain a named HTML character entity. Numbered entities are already forbidden through #.
  • It must contain no more than 256 bytes when encoded in UTF-8.

Write a function that takes in a string (or whatever data type your language normally uses to manipulate text) and outputs a boolean indicating whether the string is a valid Wikipedia article title. You are permitted to use networking libraries to download and read from any page that I linked in this question.

\$\endgroup\$
7
  • \$\begingroup\$ true/false or yes/no? The latter will save me some bytes :p \$\endgroup\$
    – Leaky Nun
    Commented Apr 3, 2016 at 8:34
  • 1
    \$\begingroup\$ This question needs a lot of work: it contains multiple links to required external data which is likely to change, and has several unclear requirements. \$\endgroup\$
    – feersum
    Commented Apr 3, 2016 at 8:35
  • 1
    \$\begingroup\$ For the second condition, is the Greek small letter eta (η) allowed? \$\endgroup\$
    – Leaky Nun
    Commented Apr 3, 2016 at 8:36
  • \$\begingroup\$ @feersum Please explain all of what is unclear, so I can fix it. The external data will be referenced by the revision of those pages at the moment I made this question \$\endgroup\$
    – EMBLEM
    Commented Apr 3, 2016 at 15:54
  • \$\begingroup\$ I am not going to list all that is unclear as there is too much. Here's a few questions to start with: 1. Does may not contain more than one consecutive space or underscore apply to underscore followed by space, or space followed by undescore? 2. What counts as an HTML entity? Only a finite list of them, any characters between & and #, or ... ? 3. No reference is given for which Unicode characters count as spaces or lowercase letters. \$\endgroup\$
    – feersum
    Commented Apr 3, 2016 at 16:03

1 Answer 1

4
\$\begingroup\$

Retina, 1221 bytes

^(?=...:|\w|(^|.*/)\.\.?(/.*|$)|.*([#<>\[\]|{}]|  |__|%[0-9a-fA-F]{2}|&#(\d{4}|[0-9a-fA-F]{4}|a(mp|pos)|(l|g|quo)t);|~~~)|[ _]*$|.{257,}).*
no
^(?!no$).+
yes

where ... is:

(w(ik(i(s(pecies|ource)|v(ersity|oyage)|([mp]edi|dat)a|(book|new)s|quote|tech)|t(ionary)?)|m(i[dlnt]|c[ahz]|a[ru]|b[de]|d[ek]|h[ku]|n[lo]|r[su]|u[ak]|fi?|mk|pl|se|tw)|ar?|uu|o)?|m(e(dia(wikiwiki|zilla)|t(awikipedi)?a)|a(p-bms|i)|[klnr]|[ghi]|[iz]n|[st]|yv?|df|hr|rj|us|wl)|s([degh]|t(rategy|q)|i(mple)?|[klmnoqr]|c(n?|o)|pecies|[uvw]|[st]|ah?|rn|zl)|b(a(t-smg|r?)|ug(zilla)?|e-tarask|[eghi]|[mno]|[rs]|cl|jn|py|xr)|t([aghw]|e(stwiki|t)?|[iklno]|oollabs|[rst]|yv?|pi|um)|c(o(mmons)?|h[ory]?|[es]b?|bk-zam|[uvy]|rh?|do|kb|a)|p(habricator|a[gmp]?|n[bt]|[st]|f?l|ih?|cd|dc|ms)|n(ds(-nl)?|[glno]|a[hp]?|[vy]|ew?|ov|rm|so)|a([afkm]|r[cz]?|[vyz]|ng?|st?|z?b|ce|dy|ls)|k([gijklmnor]|[uvwy]|a[ab]?|sh?|bd|oi|rc)|z(h(-(classical|min-nan|yue))?|e?a|u)|f([afijory]|oundation|iu-vro|r[pr]|ur)|l([gi]|[no]|ad?|be?|tg?|ez|ij|mo|rc|v)|r([nw]|o(a-(tara|rup))?|my?|ue?)|g(a[gn]?|[dln]|o[mt]|[uv]|lk)|h([eiortu]|a[kw]?|[yz]|if|sb)|i([adegikostu]|ncubator|lo)|v(e(c?|p)|oy?|ls|i)|d([vz]|[ae]|iq|sb)|e([elnostu]|ml|xt)|u([gz]|[kr]|dm)|j([av]|bo)|x(al|mf|h)|[nbqsvcdm]|o[cmrs]|y[io]|qu|)

(I used this regex optimizer and manutally typed in all the data)

Try it online without the ...!

\$\endgroup\$
1
  • \$\begingroup\$ Does this test for all the lowercase letters? There are a lot more than 26 of them, there are 1984 of them according to that file. \$\endgroup\$
    – marinus
    Commented Apr 22, 2016 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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