Problem with regex for Android Useragent

Asked

Viewed 65 times

1

I’m trying to create a regex ( to use in PHP ) that can match the groups correctly and extract the information I need. In this case is to get information from android useragent.

When the useragent is like this:

User-Agent: Mozilla/5.0 (Linux; Android 5.0.2; SM-G530BT Build/LRX22G; wv) Applewebkit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/56.0.2924.87 Mobile Safari/537.36

certain, but has useragent that has an extra die, the "en" , like this:

User-Agent: Mozilla/5.0 (Linux; U; Android 4.1.2; en; LG-E467f Build/JZO54K) Applewebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

I couldn’t because the "en" is optional.

Follow what I’ve tried https://regex101.com/r/6TRsM2/1

my regex is this:

(?P<browser>Android)\s(?P<major_version>\d+)\.(?P<minor_version>\d+\.?\d?);(?P<lang>\spt-br)?(?P<device>[ \w\-?]+)\s

Can someone help me finish this regex? and I need it to marry all the groups, and when I don’t have the en, just come back empty.

1 answer

0


Try using this Regex

(?P<browser>Android)\s(?P<major_version>\d+)\.(?P<minor_version>\d+\.?\d?);(?P<lang>\spt-br){0,1};{0,1}(?P<device>[ \w\-?]+)\s

You can test how it works here.

What I did differently?
I included {0,1} after the [...](?P<lang>\spt-br) and ;{0,1} making the existence of pt-br; after the minor version, it will now capture only if there is en.

Considerations
I do not know if there is the possibility, but it is good to consider the existence of other browsers and other lang’s, if you have this possibility use the operator "OR" (|) as in this example.

  • You’re right, I didn’t remember what other languages might come from or even another user agent structure. The ideal was one that would marry any lang that came. But in the moment, for what I need it worked. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.