【问题标题】:Escaping in regex expression python [closed]在正则表达式python中转义[关闭]
【发布时间】:2020-09-06 07:01:26
【问题描述】:

我希望从以下数据字段中提取 id 标签:

{"purchased_at":"2020-04-21T05:55:30.000Z","product_desc":"Garnier 2019 Shampoo","onhold":{"copyright":true,"country_codes":["ABC"],"scope":"poss"},"id":"8745485"}

当我使用'"id":\s*"(.*?)"' 时遇到此字段时,我使用的正则表达式会中断。

因为,只有部分字段有这样的额外保留标签:

{"purchased_at":"2020-04-21T05:55:30.000Z","product_desc":"All clear 2019 \n ","id":"7462764"}

整个文件的格式为:

{"info":[{"purchased_at":"","product_desc":"","id":""}{..}]}

【问题讨论】:

  • 这看起来像 JSON,你应该使用 json 模块,而不是正则表达式。
  • 并且正则表达式问题不清楚。 “休息”到底是什么意思?
  • @mkrieger1 ID = re.search(id_pattern, match.group(0)) 当我尝试这个时,我得到 Nonetype has no group object。

标签: python python-3.x regex regex-group regex-greedy


【解决方案1】:

您可以导入 json 库以提取所需的键值 (id),而不是使用正则表达式:

import json
str = '{"purchased_at":"2020-04-21T05:55:30.000Z","product_desc":"Garnier 2019 Shampoo","onhold":{"copyright":true,"country_codes":["ABC"],"scope":"poss"},"id":"8745485"}'

js = json.loads(str)

for i in js:
      if i == 'id':
            print(js[i])

>>>
8745485   

更新:如果您需要使用与正则表达式相关的方法来查找,那么使用具有适当模式的re 库的search 函数可能会有所帮助:

import re
str = '{"purchased_at":"2020-04-21T05:55:30.000Z","product_desc":"Garnier 2019 Shampoo","onhold":{"copyright":true,"country_codes":["ABC"],"scope":"poss"},"id":"8745485"}'

s = re.search('id":"(.+?)"', str)

if s:
    print( s.group(1) )

>>>
8745485 

【讨论】:

  • 我明白了。但我正在寻找一个正则表达式的答案!
  • 需要再说一遍:不要为此使用正则表达式。
  • 那么,第一部分没问题,我应该删除 update 部分,你是说这个@tripleee 吗?
  • 不,只是想告诉 OP 他们绝对不想要他们坚持想要的东西。
【解决方案2】:

只需使用re模块中的findall方法即可提取数据。

import re
line='{"purchased_at":"2020-04-21T05:55:30.000Z","product_desc":"Garnier 2019 Shampoo","onhold":{"copyright":true,"country_codes":["ABC"],"scope":"poss"},"id":"8745485"}'
print(re.findall('"id":\s*"(.*?)"',line))

输出

['8745485']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多