Page 1 of 1
HowTo find an English (en) within Accept-Language header
Posted: Mon Apr 20, 2009 2:53 am
by evgeny1976
Hello there Regex Experts!
Your help end experience is needed
I have a following SIP header:
Accept-Language: en, da, en-gb;q=0.8, fr, en;q=0.7
Accept-Language: en
Accept-Language: fr, en, da
I need to find all varieties of english within. That is : en, en-gb;q=0.8 and en;q=0.7
What will be a regex for doing so?
PS: Regex engine runs on FreeBSD machine
Thanks in advance
Re: HowTo find an English (en) within Accept-Language header
Posted: Mon Apr 20, 2009 4:56 am
by jazz090
this maybe an understatement but if all of em contain "en" just run a simple regex for "/en/"
Re: HowTo find an English (en) within Accept-Language header
Posted: Mon Apr 20, 2009 5:02 am
by prometheuzz
Since this is a PHP/regex forum, here's a PHP way:
Code: Select all
preg_match_all('/en[^\s,]*/', 'Accept-Language: en, da, en-gb;q=0.8, fr, en;q=0.7', $matches);
print_r($matches);
/*
Array
(
[0] => Array
(
[0] => en
[1] => en-gb;q=0.8
[2] => en;q=0.7
)
)
*/
Re: HowTo find an English (en) within Accept-Language header
Posted: Mon Apr 20, 2009 5:40 am
by evgeny1976
Hi All!
Thanks a lot for your help!
The simplest
en solution works!
Somehow Eclipse QuickRex tool confused me a little bit

Re: HowTo find an English (en) within Accept-Language header
Posted: Mon Apr 20, 2009 5:47 am
by prometheuzz
evgeny1976 wrote:Hi All!
Thanks a lot for your help!
The simplest
en solution works!
Somehow Eclipse QuickRex tool confused me a little bit

The regex "en" will only match the characters "en" and not the entire substring "en-gb;q=0.8". But if that is okay for you, there really is no need for regex: a simple string operation like stristr():
http://www.w3schools.com/PHP/func_string_stristr.asp
Re: HowTo find an English (en) within Accept-Language header
Posted: Mon Apr 20, 2009 3:16 pm
by jazz090
yh but its really irrelevant in here as nothing other than "en" will appear becuase its one of a kind