如何在 Python 中使用正则表达式来 return 这些数字?
How can I use regex in Python to return these numbers?
我将以下代码作为字符串变量存储在 Python 中。如何使用正则表达式和 re.findall('', text)
来解析下面代码中 "attributeLookup" 查找下的五个 9 位数字(均以“305...”开头)?
var PRO_META_JSON = {
"attributeDefinition":{
"defaultSku":305557121,
"attributeListing":[{
"label":"Finish",
"defaultIndex":0,
"options":[
"White::f33b4086",
"Beige::8e0900fa",
"Blue::3c3a4707",
"Orange::1d8cb503",
"Spring Green::dd5e599a"
]
}],
"attributeLookup":[
[0,305557121],
[1,305557187],
[2,305557696],
[3,305557344],
[4,305696435]
]
}
};
在 findall 中你想要 select 数字 0 到 9 超过 9 个字符,就像这样。这仍然会更好地使用 json 模块而不是存储为字符串。
我对 python 正则表达式非常有用的测试器可以在这里找到
re.findall('[0-9]{9}', PRO_META_JSON.split('attributeLookup')[1])
这是一种方法。首先解析您的字符串以获取 json 对象(最外括号内的所有内容)。然后使用 json 模块解码 json 对象并访问您需要的内容。
astr = '''var PRO_META_JSON = {
"attributeDefinition":{
"defaultSku":305557121,
"attributeListing":[{
"label":"Finish",
"defaultIndex":0,
"options":[
"White::f33b4086",
"Beige::8e0900fa",
"Blue::3c3a4707",
"Orange::1d8cb503",
"Spring Green::dd5e599a"
]
}],
"attributeLookup":[
[0,305557121],
[1,305557187],
[2,305557696],
[3,305557344],
[4,305696435]
]
}
};'''
import re
import json
pat = re.compile('^[^\{]*(\{.*\});.*$', re.MULTILINE|re.DOTALL)
json_str = pat.match(astr).group(1)
d = json.loads(json_str)
for x in d['attributeDefinition']['attributeLookup']:
print x[1]
# 305557121
# 305557187
# 305557696
# 305557344
# 305696435
您可以只使用内置的 json 库来解析它。我假设您已经摆脱了 Javascript:
import json
input = """{
"attributeDefinition":{
"defaultSku":305557121,
"attributeListing":[{
"label":"Finish",
"defaultIndex":0,
"options":[
"White::f33b4086",
"Beige::8e0900fa",
"Blue::3c3a4707",
"Orange::1d8cb503",
"Spring Green::dd5e599a"
]
}],
"attributeLookup":[
[0,305557121],
[1,305557187],
[2,305557696],
[3,305557344],
[4,305696435]
]
}
}"""
data = json.loads(input)
# Get a list you can do stuff with. This gives you:
# [[0, 305557121], [1, 305557187], [2, 305557696], [3, 305557344], [4, 305696435]]
els = data['attributeDefinition']['attributeLookup']
for el in els:
# Each el looks like: [0, 305557121]
print(el[1])
string = '''var PRO_META_JSON = {
"attributeDefinition":{
"defaultSku":305557121,
"attributeListing":[{
"label":"Finish",
"defaultIndex":0,
"options":[
"White::f33b4086",
"Beige::8e0900fa",
"Blue::3c3a4707",
"Orange::1d8cb503",
"Spring Green::dd5e599a"
]
}],
"attributeLookup":[
[0,305557121],
[1,305557187],
[2,305557696],
[3,305557344],
[4,305696435]
]
}
};'''
import json
data = json.loads(string.split('=', 1)[1].strip(';'))
for d in data['attributeDefinition']['attributeLookup']:
print(d[1])
不知道为什么要使用正则表达式。你也开车去拜访你的邻居吗?
我将以下代码作为字符串变量存储在 Python 中。如何使用正则表达式和 re.findall('', text)
来解析下面代码中 "attributeLookup" 查找下的五个 9 位数字(均以“305...”开头)?
var PRO_META_JSON = {
"attributeDefinition":{
"defaultSku":305557121,
"attributeListing":[{
"label":"Finish",
"defaultIndex":0,
"options":[
"White::f33b4086",
"Beige::8e0900fa",
"Blue::3c3a4707",
"Orange::1d8cb503",
"Spring Green::dd5e599a"
]
}],
"attributeLookup":[
[0,305557121],
[1,305557187],
[2,305557696],
[3,305557344],
[4,305696435]
]
}
};
在 findall 中你想要 select 数字 0 到 9 超过 9 个字符,就像这样。这仍然会更好地使用 json 模块而不是存储为字符串。
我对 python 正则表达式非常有用的测试器可以在这里找到
re.findall('[0-9]{9}', PRO_META_JSON.split('attributeLookup')[1])
这是一种方法。首先解析您的字符串以获取 json 对象(最外括号内的所有内容)。然后使用 json 模块解码 json 对象并访问您需要的内容。
astr = '''var PRO_META_JSON = {
"attributeDefinition":{
"defaultSku":305557121,
"attributeListing":[{
"label":"Finish",
"defaultIndex":0,
"options":[
"White::f33b4086",
"Beige::8e0900fa",
"Blue::3c3a4707",
"Orange::1d8cb503",
"Spring Green::dd5e599a"
]
}],
"attributeLookup":[
[0,305557121],
[1,305557187],
[2,305557696],
[3,305557344],
[4,305696435]
]
}
};'''
import re
import json
pat = re.compile('^[^\{]*(\{.*\});.*$', re.MULTILINE|re.DOTALL)
json_str = pat.match(astr).group(1)
d = json.loads(json_str)
for x in d['attributeDefinition']['attributeLookup']:
print x[1]
# 305557121
# 305557187
# 305557696
# 305557344
# 305696435
您可以只使用内置的 json 库来解析它。我假设您已经摆脱了 Javascript:
import json
input = """{
"attributeDefinition":{
"defaultSku":305557121,
"attributeListing":[{
"label":"Finish",
"defaultIndex":0,
"options":[
"White::f33b4086",
"Beige::8e0900fa",
"Blue::3c3a4707",
"Orange::1d8cb503",
"Spring Green::dd5e599a"
]
}],
"attributeLookup":[
[0,305557121],
[1,305557187],
[2,305557696],
[3,305557344],
[4,305696435]
]
}
}"""
data = json.loads(input)
# Get a list you can do stuff with. This gives you:
# [[0, 305557121], [1, 305557187], [2, 305557696], [3, 305557344], [4, 305696435]]
els = data['attributeDefinition']['attributeLookup']
for el in els:
# Each el looks like: [0, 305557121]
print(el[1])
string = '''var PRO_META_JSON = {
"attributeDefinition":{
"defaultSku":305557121,
"attributeListing":[{
"label":"Finish",
"defaultIndex":0,
"options":[
"White::f33b4086",
"Beige::8e0900fa",
"Blue::3c3a4707",
"Orange::1d8cb503",
"Spring Green::dd5e599a"
]
}],
"attributeLookup":[
[0,305557121],
[1,305557187],
[2,305557696],
[3,305557344],
[4,305696435]
]
}
};'''
import json
data = json.loads(string.split('=', 1)[1].strip(';'))
for d in data['attributeDefinition']['attributeLookup']:
print(d[1])
不知道为什么要使用正则表达式。你也开车去拜访你的邻居吗?