Hello,
for my CGI application I'm writing a function to get the browser's preferred language (supplied in the HTTP_ACCEPT_LANGUAGE variable). I want to find all language tags in this variable with regular expressions (The general pattern of a language tag is defined in RFC1766). EBNF from RFC1766 ('1*8ALPHA' means one to eight ASCII chars):
Language-Tag = Primary-tag *( "-" Subtag )
Primary-tag = 1*8ALPHA
Subtag = 1*8ALPHA
I wrote this regular expression for a language tag:
(([a-z]{1,8})(-[a-z]{1,8})*)
If i use this expression, Python's re module supplies the following
>>import re
>>re.findall("(([a-z]{1,8})(-[a-z]{1,8})*)", "x-pig-latin en-us de-de en", re.IGNORECASE)
[('x-pig-latin', 'x', '-latin'), ('en-us', 'en', '-us'), ('de-de', 'de', '-de'), ('en', 'en', '')]
The result is correct. But I only need complete matches like 'de-de' or 'x-pig-latin'. Can I assume that the first match of a group is always the most complete one? Or is there a flag telling re to show the most complete matches?
Stefan