我在文本文件中有一些JSON数据,如:
..."priority":1,...
..."priority":3,...
..."priority":5,...
我想要做的是匹配优先级小于或等于3的所有行。
所以比赛将是:
..."priority":1,...
..."priority":3,...
所以我的基本正则表达式可能如下:
/"priority":[0-9],/
和一个简单的脚本来测试这个
const regex = /"priority":[0-9],/;
const str = '{"dateCreated":"2016-12-31T06:41:32.298Z","pid":15154,"count":1,"uid":"77d55631-36ab-4805-aa17-1c984c8d4d04","priority":1,"isRead":false,"line":"foo bar baz"}';
console.log(regex.test(str)); // true
所以这将匹配任何数字0-9,但我怎么能匹配X及以下?哪个X可能是2位数?
这是一种简洁的方式吗?
文件中的原始数据如下所示:
{"dateCreated":"2016-12-31T20:31:42.928Z","pid":5502,"count":0,"uid":"b21ac5eb-e25f-411c-80e9-955ff6eab964","priority":2,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:43.058Z","pid":5502,"count":1,"uid":"19f69863-7bf7-45f1-99b8-30fe332e88b9","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:43.262Z","pid":5502,"count":2,"uid":"ee8ca050-d1b5-4787-8d9f-78b17a9c4e9e","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:43.462Z","pid":5502,"count":3,"uid":"40fdbf37-0caf-4dde-b6fa-8522cd333b6e","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:43.666Z","pid":5502,"count":4,"uid":"d1928b11-e93c-413a-8e4e-49ac8a7b38b5","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:43.865Z","pid":5502,"count":5,"uid":"9fdff4bc-5126-43f9-85c6-d1212bac777b","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:44.071Z","pid":5502,"count":6,"uid":"3058cc78-eea6-4de9-96de-69c107b73724","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:44.271Z","pid":5502,"count":7,"uid":"52899ff3-2c17-4392-9409-a24152d4d15c","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:44.468Z","pid":5502,"count":8,"uid":"a775cd96-be9e-4713-83d3-92441d73443d","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:44.675Z","pid":5502,"count":9,"uid":"4fedb82b-1fb9-4049-870e-ff3bdf32fd5e","priority":2,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:44.877Z","pid":5502,"count":10,"uid":"c3fd144c-0182-4777-80a8-ce5db0da6525","priority":2,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:45.075Z","pid":5502,"count":11,"uid":"70fde6ea-f57c-4d2c-9eb0-d2b04fae23f2","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:45.277Z","pid":5502,"count":12,"uid":"540db3c7-bfe0-4d09-b4b6-d8922455c8d5","priority":3,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:45.483Z","pid":5502,"count":13,"uid":"1eca7c3b-be01-4457-885f-b7cb807952aa","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:45.683Z","pid":5502,"count":14,"uid":"d7f3cf02-e302-49d5-9977-fac592349335","priority":1,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:45.881Z","pid":5502,"count":15,"uid":"8760aa12-5c10-456f-8ae1-b570bc939ff7","priority":2,"isRead":false,"line":"foo bar baz"}
{"dateCreated":"2016-12-31T20:31:46.087Z","pid":5502,"count":16,"uid":"152a51da-d253-4245-999d-6518318c305d","priority":1,"isRead":false,"line":"foo bar baz"}
(这是新行单独的JSON数据)
答案 0 :(得分:2)
regex
这不是一个好例子。相反,您应该使用json模块加载文件,然后只测试" priority"的值。
import json
with open('./sample_json.json', 'r') as json_data_file:
json_data = json_data_file.readlines()
json_data_file.closed
使用列表推导,我们会为dicts
创建一个json_data
列表,每列一个:
json_array = [json.loads(line) for line in json_data]
最后,我们为dict
中的每个json_array
运行大于小于的测试并打印每个结果:对于json_array中的行:
if line['priority'] < 20:
print("Success!")
请注意,我们可以通过使用
for
循环而不是列表推导来实现此目的,结果相同:
json_array = []
for line in json_data:
json_array.append(json.loads(line))
要了解有关json
模块的更多信息,请在此处阅读:
https://docs.python.org/3/library/json.html
为了便于复制/粘贴,这里是不间断的代码:
import json
with open('./sample_json.json', 'r') as json_data_file:
json_data = json_data_file.readlines()
json_data_file.closed
# Using list comprehension to populate json_array
json_array = [json.loads(line) for line in json_data]
# Using for loop to populate json_array
json_array = []
for line in json_data:
json_array.append(json.loads(line))
for line in json_array:
if line['priority'] < 20:
print("Success!")
请注意,使用已解析的JSON作为dict
,您可以访问JSON每行的任何键:
for item in json_array:
for key in item.keys():
print("KEY:", key, "VALUE:", json_array[0][key])
print()
答案 1 :(得分:1)
使用JSON加载/解析等,这绝对是正确的方法,但在使用正则表达式解决这个问题
蛮力方法,如下:
"\"priority\":(1|2|3|4),"
对于较小的整数应该可以正常工作,并且可能非常容易出错。