Extract model from device name

Mozilla/2.0 (Linux; Android 2.0; KK; SAMSUNG SM-G900H Build/LRX21T) AppleWebKit

Must be extracted SAMSUNG SM-G900H Build/LRX21T.

Mozilla/5.0 (Linux; Android 5.0; SAMSUNG SM-G900H Build/LRX21T) AppleWebKit

Must be extracted SAMSUNG SM-G900H Build/LRX21T.

Mozilla/5.0 (Linux; Android 5.1; XT1032 Build/LPBS23.13-56-2; wv) AppleWebKit

It is required to extract XT1032 Build/LPBS23.13-56-2.

The logic is this:

1. Take first [(.....)]
2. Take text after last semicolon [;]
3. Or take text after last semicolon [;] and before [; wv]

What I have now:

.*?\(.*;(?<model>.*?)(; wv)?\)

But this works for the first 2 examples, and returns for the third example wv.

+4
source share
3 answers
^(.*)\(.*;(?<model>(?!( wv))[^;()]*(\(.??\))?[^;)]*)+?(;.*)?\).*$

corresponds to

  • Mozilla / 5.0 (Linux, Android 5.1; XT1032 Build / LPBS23.13-56-2 ; wv) AppleWebKit
  • Mozilla / 5.0 (Linux, Android 5.0; SAMSUNG SM-G900H Build / LRX21T ) AppleWebKit
  • Mozilla / 5.0 (Linux, Android 5.0; SAMSUNG SM-G900H Build / LRX21T ) AppleWebKit / 537.36 (KHTML, e.g. Gecko) SamsungBrowser / 4.0 Chrome / 44.0.2403.133 Mobile Safari / 537.36 li>
  • Mozilla/5.0 (Linux, Android 6.0.1; Moto G (4) Build/MPJ24.139-23.1) AppleWebKit/537.36 (KHTML, , Gecko) Chrome/64.0.3282.137 Mobile Safari/537.36

lookAhead ((?! wv)) .

, , ( /gi -Flags [, ).

, :

  • lookahead (?!...)
  • ?? ( "?" , )

EDIT: , no ')' EDIT2: ,

+2

:

;\s*(?<model>([^;)])*)

, .

Mozilla/5.0 (Linux

, , .

+1

:

string[] parts = myUserAgent.Split(";");

"SAMSUNG....":

string temp = parts[3];

in the end, use the "replace" function to remove unintentional characters.

0
source

Source: https://habr.com/ru/post/1695226/


All Articles