1# Copyright (C) 2006-2009, Google, International Business Machines Corporation and others. All Rights Reserved. 2# Regex for recognizing RFC 4646 well-formed tags 3# http://www.rfc-editor.org/rfc/rfc4646.txt 4# http://tools.ietf.org/html/draft-ietf-ltru-4646bis-21 5 6# The structure requires no forward references, so it reverses the order. 7# It uses Java/Perl syntax instead of the old ABNF 8# The uppercase comments are fragments copied from RFC 4646 9 10# Note: the tool requires that any real "=" or "#" or ";" in the regex be escaped. 11 12$alpha = [a-z] ; # ALPHA 13$digit = [0-9] ; # DIGIT 14$alphanum = [a-z 0-9] ; # ALPHA / DIGIT 15$x = x ; # private use singleton 16$singleton = [a-w y-z] ; # other singleton 17$s = [-_] ; # separator -- lenient parsers will use [-_] -- strict will use [-] 18 19# Now do the components. The structure is slightly different to allow for capturing the right components. 20# The notation (?:....) is a non-capturing version of (...): so the "?:" can be deleted if someone doesn't care about capturing. 21 22$language = $alpha{2,8} | $alpha{2,3} $s $alpha{3}; 23 24 # ABNF (2*3ALPHA) / 4ALPHA / 5*8ALPHA --- note: because of how | works in regex, don't use $alpha{2,3} | $alpha{4,8} 25 # We don't have to have the general case of extlang, because there can be only one extlang (except for zh-min-nan). 26 27# Note: extlang invalid in Unicode language tags 28 29$script = $alpha{4} ; # 4ALPHA 30 31$region = $alpha{2} | $digit{3} ; # 2ALPHA / 3DIGIT 32 33$variant = (?: $alphanum{5,8} | $digit $alphanum{3} ) ; # 5*8alphanum / (DIGIT 3alphanum) 34 35$extension = $singleton (?: $s $alphanum{2,8} )+ ; # singleton 1*("-" (2*8alphanum)) 36 37$privateUse = $x (?: $s $alphanum{1,8} )+ ; # "x" 1*("-" (1*8alphanum)) 38 39# Define certain grandfathered codes, since otherwise the regex is pretty useless. 40# Since these are limited, this is safe even later changes to the registry -- 41# the only oddity is that it might change the type of the tag, and thus 42# the results from the capturing groups. 43# http://www.iana.org/assignments/language-subtag-registry 44# Note that these have to be compared case insensitively, requiring (?i) below. 45 46$grandfathered = en $s GB $s oed 47 | i $s (?: ami | bnn | default | enochian | hak | klingon | lux | mingo | navajo | pwn | tao | tay | tsu ) 48 | no $s (?: bok | nyn ) 49 | sgn $s (?: BE $s (?: fr | nl) | CH $s de ) 50 | zh $s min $s nan; 51 52# old: | zh $s (?: cmn (?: $s Hans | $s Hant )? | gan | min (?: $s nan)? | wuu | yue ); 53# For well-formedness, we don't need the ones that would otherwise pass. 54# For validity, they need to be checked. 55 56# $grandfatheredWellFormed = (?: 57# art $s lojban 58# | cel $s gaulish 59# | zh $s (?: guoyu | hakka | xiang ) 60# ); 61 62# Unicode locales: but we are shifting to a compatible form 63# $keyvalue = (?: $alphanum+ \= $alphanum+); 64# $keywords = ($keyvalue (?: \; $keyvalue)*); 65 66# We separate items that we want to capture as a single group 67 68$variantList = $variant (?: $s $variant )* ; # special for multiples 69$extensionList = $extension (?: $s $extension )* ; # special for multiples 70 71$langtag = (?: ( $language ) 72 (?: $s ( $script ) )? 40% 73 (?: $s ( $region ) )? 40% 74 (?: $s ( $variantList ) )? 10% 75 (?: $s ( $extensionList ) )? 5% 76 (?: $s ( $privateUse ) )? 5%); 77 78# Here is the final breakdown, with capturing groups for each of these components 79# The variants, extensions, grandfathered, and private-use may have interior '-' 80 81$root = (?i) # case-insensitive 82 (?: 83 $langtag 90% 84 | ( $privateUse ) 5% 85 | ( $grandfathered ) 5%) 86# (?: \@ $keywords )? 5% 87 ;