In: Computer Science
For example
My IP address 128. 0. 0. 1
My IP address 53. 20. 62. 201
My IP address 77. 122. 180. 76
My IP address 54. 33. 11. 11
My IP address 33. 33. 2. 11
My IP address 202. 29. 205. 69
My IP address 215. 18. 15. 185
My IP address 56. 66. 33. 30
How do you write a regular expression that covers all IP addresses and display all list using a loop in Python
Snipptes of source code:
source code:
# A Regular Expression is a special text string for describing a search pattern.
import re # import regular expression module
# storing all ip_adresses in list variable "Ip_addresses"
Ip_addresses = ["128.0.0.1",
"53.20.62.201",
"77.122.180.76",
"54.33.11.11",
"33.33.2.11",
"202.29.205.69",
"215.18.15.185",
"56.66.33.30"]
reg_exp_for_ip = "^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$" # regular expression to find Ip_addresses
#go through the Python official documentation for deeply understanig to these regular notation
for ip in Ip_addresses:
print(re.findall(r"[0-9]+(?:\.[0-9]+){3}", ip))
Output:
NOTE: If you satisfied with solution then like and review in comment section