Регулярное выражение здесь не нужно, потому что IP-адреса могут не требовать проверки, если я правильно понимаю проблему, мы просто хотим получить значение "ip_prefix"
, но если вы хотите сделать это с помощью регулярного выражения, этого может быть достаточно:
"ip_prefix": "(.+?)"
Test
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\"ip_prefix\": \"(.+?)\""
test_str = ("{\n"
" \"syncToken\": \"1560279544\",\n"
" \"createDate\": \"2019-06-11-18-59-04\",\n"
" \"prefixes\": [\n"
" {\n"
" \"ip_prefix\": \"18.208.0.0/13\",\n"
" \"region\": \"us-east-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"52.95.245.0/24\",\n"
" \"region\": \"us-east-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"52.194.0.0/15\",\n"
" \"region\": \"ap-northeast-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"54.155.0.0/16\",\n"
" \"region\": \"eu-west-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"54.196.0.0/15\",\n"
" \"region\": \"us-east-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"99.78.170.0/23\",\n"
" \"region\": \"ap-southeast-2\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"52.94.22.0/24\",\n"
" \"region\": \"us-gov-east-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"52.95.255.112/28\",\n"
" \"region\": \"us-west-2\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"13.210.0.0/15\",\n"
" \"region\": \"ap-southeast-2\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"52.94.17.0/24\",\n"
" \"region\": \"eu-central-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"52.95.154.0/23\",\n"
" \"region\": \"eu-west-3\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"52.95.212.0/22\",\n"
" \"region\": \"ap-southeast-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"54.239.0.240/28\",\n"
" \"region\": \"eu-west-2\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"54.241.0.0/16\",\n"
" \"region\": \"us-west-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"184.169.128.0/17\",\n"
" \"region\": \"us-west-1\",\n"
" \"service\": \"AMAZON\"\n"
" },\n"
" {\n"
" \"ip_prefix\": \"216.182.224.0/21\",\n"
" \"region\": \"us-east-1\",\n"
" \"service\": \"AMAZON\"\n\n"
"...")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.