HTTP Accept-Language标头验证正则表达式

时间:2018-02-07 16:12:43

标签: regex http header pcre

我想使用PCRE正则表达式来验证HTTP Accept-Language标头值,我想出了这个正则表达式:

(?i:(?:\*|[a-z\-]{2,5})(?:;q=\d\.\d)?,)+(?:\*|[a-z\-]{2,5})(?:;q=\d\.\d)?

Accept-Language标头值的两个示例:

fr-CH,fr;q=0.9,en;q=0.8,de;q=0.7,*;q=0.5  

en-US,en;q=0.9

是否有更优化的正则表达式?

1 个答案:

答案 0 :(得分:1)

从某种意义上说,为了使用捕获组和递归来替换,会有一个正则表达式:

;; LABEL "EN-F" "Callhome Female" "Callhome Female Caller"
;; LABEL "EN-M" "Callhome Male" "Callhome Male Caller"
;; LABEL "SW-F" "Switchboard Female" "Switchboard Female Caller"
;; LABEL "SW-M" "Switchboard Male" "Switchboard Male Caller"
en_4156  <O,en,F,en-F>  OH YEAH
en_4156  <O,en,F,en-F>  WELL I AM GOING TO HAVE MINE IN TWO MORE CLASSES
en_4156  <O,en,F,en-F>  NO I AM NOT WELL THEN I HAVE TO TAKE MY EXAMS MY ORALS BUT
en_4156  <O,en,F,en-F>  THAT IS KIND OF WHAT I WOULD LIKE TO DO
en_4156  <O,en,F,en-F>  I MIGHT EVEN WANT TO GO ON AND GET MY P H D
en_4156  <O,en,F,en-F>  IT IS JUST THAT
en_4156  <O,en,F,en-F>  I DO NOT KNOW IT IS SO COMFORTABLE WHERE I AM AND AND IF I