HowTo find an English (en) within Accept-Language header

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
evgeny1976
Forum Newbie
Posts: 2
Joined: Mon Apr 20, 2009 2:48 am

HowTo find an English (en) within Accept-Language header

Post 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
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: HowTo find an English (en) within Accept-Language header

Post by jazz090 »

this maybe an understatement but if all of em contain "en" just run a simple regex for "/en/"
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: HowTo find an English (en) within Accept-Language header

Post 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
        )
 
)
*/
evgeny1976
Forum Newbie
Posts: 2
Joined: Mon Apr 20, 2009 2:48 am

Re: HowTo find an English (en) within Accept-Language header

Post by evgeny1976 »

Hi All!
Thanks a lot for your help!
The simplest en solution works!
Somehow Eclipse QuickRex tool confused me a little bit :-)
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: HowTo find an English (en) within Accept-Language header

Post 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
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: HowTo find an English (en) within Accept-Language header

Post by jazz090 »

yh but its really irrelevant in here as nothing other than "en" will appear becuase its one of a kind
Post Reply