fork download
  1. import re
  2. p = re.compile(r'^(\d+)\s[^(]+\(([^()]+)\)')
  3. test_str = "70849 mozilla/5.0(linux;u;android4.2.1;zh-cn)applewebkit/534.30(khtml,likegecko)version/4.0mobilesafari/534.30"
  4. print(p.findall(test_str))
  5. # or using re.search if the number is not at the beginning of the string
  6. m = re.search(r'(\d+)\s[^(]+\(([^()]+)\)', test_str)
  7. if m:
  8. print("Number: {0}\nString: {1}".format(m.group(1), m.group(2)))
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
[('70849', 'linux;u;android4.2.1;zh-cn')]
Number: 70849
String: linux;u;android4.2.1;zh-cn