# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^.*Download:\s{3,5}\K\d{1,3}\.\d{0,2}(?= Mbps)"
test_str = (" Speedtest by Ookla\n\n"
" Server: Fastweb SpA - Milan (id: 7839)\n"
" ISP: Wind Tre\n"
"Idle Latency: 29.13 ms (jitter: 7.59ms, low: 22.73ms, high: 33.85ms)\n"
" Download: 238.14 Mbps (data used: 384.0 MB)\n"
" 603.92 ms (jitter: 87.34ms, low: 37.24ms, high: 1203.74ms)\n"
" Upload: 46.48 Mbps (data used: 43.4 MB)\n"
" 172.46 ms (jitter: 54.67ms, low: 37.41ms, high: 304.20ms)\n"
" Packet Loss: 0.0%\n"
" Result URL: https://www.speedtest.net/result/c/f4f1d464-82c6-4980-8b3f-38358ccedbfb\n\n"
" Speedtest by Ookla\n\n"
" Server: Fastweb SpA - Milan (id: 7839)\n"
" ISP: Wind Tre\n"
"Idle Latency: 23.63 ms (jitter: 0.51ms, low: 23.25ms, high: 25.28ms)\n"
" Download: 209.23 Mbps (data used: 356.9 MB)\n"
" 629.97 ms (jitter: 87.98ms, low: 34.60ms, high: 966.30ms)\n"
" Upload: 44.29 Mbps (data used: 39.4 MB)\n"
" 179.07 ms (jitter: 54.45ms, low: 55.78ms, high: 288.01ms)\n"
" Packet Loss: 0.0%\n"
" Result URL: https://www.speedtest.net/result/c/ab0ea00c-28c1-4ab0-a65e-3e49ec70160f\n\n"
" Speedtest by Ookla\n\n"
" Server: Fastweb SpA - Milan (id: 7839)\n"
" ISP: Wind Tre\n"
"Idle Latency: 23.36 ms (jitter: 5.38ms, low: 22.38ms, high: 32.22ms)\n"
" Download: 190.75 Mbps (data used: 341.4 MB)\n"
" 811.89 ms (jitter: 81.96ms, low: 38.70ms, high: 1557.16ms)\n"
" Upload: 46.68 Mbps (data used: 78.4 MB)\n"
" 177.39 ms (jitter: 57.87ms, low: 54.85ms, high: 636.20ms)\n"
" Packet Loss: 0.0%\n"
" Result URL: https://www.speedtest.net/result/c/6618ad7c-8990-4e8c-9ccf-b2d8726e5757\n\n"
" Speedtest by Ookla\n\n"
" Server: Fastweb SpA - Milan (id: 7839)\n"
" ISP: Wind Tre\n"
"Idle Latency: 23.37 ms (jitter: 0.97ms, low: 22.43ms, high: 24.39ms)\n"
" Download: 7.30 Mbps (data used: 9.5 MB)\n"
" 788.00 ms (jitter: 148.57ms, low: 195.04ms, high: 2617.58ms)\n"
" Download: FAILED\n\n"
" Speedtest by Ookla\n\n"
" Server: Fastweb SpA - Milan (id: 7839)\n"
" ISP: Wind Tre\n"
"Idle Latency: 24.34 ms (jitter: 5.23ms, low: 23.35ms, high: 32.27ms)\n"
" Download: 29.70 Mbps (data used: 36.9 MB)\n"
" 2842.76 ms (jitter: 102.51ms, low: 36.73ms, high: 3780.32ms)\n"
" Upload: 7.91 Mbps (data used: 11.4 MB)\n"
" 642.92 ms (jitter: 85.25ms, low: 168.67ms, high: 1465.30ms)\n"
" Packet Loss: 0.0%\n"
" Result URL: https://www.speedtest.net/result/c/c625078c-1efb-4d0c-86f6-47f85ee1838e\n\n"
" Speedtest by Ookla\n\n"
" Server: Fastweb SpA - Milan (id: 7839)\n"
" ISP: Wind Tre\n"
"Idle Latency: 23.40 ms (jitter: 0.75ms, low: 22.38ms, high: 24.37ms)\n"
" Download: 48.79 Mbps (data used: 53.9 MB)\n"
" 1605.18 ms (jitter: 98.93ms, low: 51.83ms, high: 2609.00ms)\n"
" Upload: 10.45 Mbps (data used: 5.4 MB)\n"
" 350.75 ms (jitter: 80.15ms, low: 60.41ms, high: 742.65ms)\n"
" Packet Loss: 0.0%\n"
" Result URL: https://www.speedtest.net/result/c/594b8f34-673c-4670-a091-56104e909153\n\n"
" Speedtest by Ookla\n\n"
" Server: Fastweb SpA - Milan (id: 7839)\n"
" ISP: Wind Tre\n"
"Idle Latency: 24.63 ms (jitter: 4.15ms, low: 24.34ms, high: 32.18ms)\n"
" Download: 58.12 Mbps (data used: 69.5 MB)\n"
" 1889.25 ms (jitter: 101.87ms, low: 96.60ms, high: 3067.85ms)\n"
" Upload: 10.45 Mbps (data used: 18.9 MB)\n"
" 591.63 ms (jitter: 88.04ms, low: 79.24ms, high: 1231.14ms)\n"
" Packet Loss: 0.0%\n"
" Result URL: https://www.speedtest.net/result/c/35fd9653-3907-4ddd-8680-8ca69b2f0de7\n\n"
" Speedtest by Ookla\n\n"
" Server: Fastweb SpA - Milan (id: 7839)\n"
" ISP: Wind Tre\n"
"Idle Latency: 22.87 ms (jitter: 1.21ms, low: 22.35ms, high: 23.94ms)\n"
" Download: 65.27 Mbps (data used: 76.1 MB)\n"
" 1603.47 ms (jitter: 97.63ms, low: 48.89ms, high: 2508.82ms)\n"
" Upload: 10.92 Mbps (data used: 8.3 MB)\n"
" 403.47 ms (jitter: 83.73ms, low: 77.31ms, high: 814.42ms)\n"
" Packet Loss: 0.0%\n"
" Result URL: https://www.speedtest.net/result/c/8de80c8e-cf49-43ee-8964-133e25c7e783\n\n"
" Speedtest by Ookla\n\n"
" Server: Fastweb SpA - Milan (id: 7839)\n"
" ISP: Wind Tre\n"
"Idle Latency: 24.64 ms (jitter: 1.16ms, low: 22.85ms, high: 25.41ms)\n"
" Download: 66.83 Mbps (data used: 77.7 MB)\n")
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html