如果正则表达式存在,则提取json日志中的字段

时间:2019-07-09 16:03:24

标签: regex regex-lookarounds regex-negation regex-group regex-greedy

我正在尝试提取以下示例数据中的字段

{"Value":{"Data":{"Items":[{"FieldType":"ABC","Value":"*****"},{"FieldType":"ACB","Value":"*****"},{"FieldType":"ABCD","Value":"*****"},,{"FieldType":"XYZ","Value":"*****"}],"EncryptedDocKey":"*****","Domain":"*****","Partner":"***","Carrier":"****"},"RequestTrackerId":"****","Message":"OK"},"Formatters":[],"ContentTypes":[],"DeclaredType":null,"StatusCode":null}

{"Value":{"Data":{"Items":[{"FieldType":"ABC","Value":"*****"},{"FieldType":"XYZ","Value":"*****"},{"FieldType":"ACD","Value":"*****"}],,"EncryptedDocKey":"*****","Domain":"*****","Partner":"***","Carrier":"****"},"RequestTrackerId":"****","Message":"OK"},"Formatters":[],"ContentTypes":[],"DeclaredType":null,"StatusCode":null}

{"Value":{"Data":{"Items":[{"FieldType":"XYZ","Value":"*****"},{"FieldType":"ACD","Value":"*****"}],,"EncryptedDocKey":"*****","Domain":"*****","Partner":"***","Carrier":"****"},"RequestTrackerId":"****","Message":"OK"},"Formatters":[],"ContentTypes":[],"DeclaredType":null,"StatusCode":null}

{"Value":{"Data":{"Items":[{"FieldType":"ABCD","Value":"*****"}],,"EncryptedDocKey":"*****","Domain":"*****","Partner":"***","Carrier":"****"},"RequestTrackerId":"****","Message":"OK"},"Formatters":[],"ContentTypes":[],"DeclaredType":null,"StatusCode":null}

我需要将FieldType (ABC,XYZ,ABCD,ACB,ACB ...)旁边的文本提取到V1,V2,V3,V4,V5等...

如果有三个字段,我有一个正则表达式可以获取,但是如果有三个以上字段,则正则表达式不映射。

我尝试过此正则表达式

.?"FieldType":"(?<V1>\w+)".+?"FieldType":"(?<V2>\w+).+?"FieldType":"(?<V3>\w+)

我也想提取出fieldtype和,EncryptedDocKey,Domain,Partner,Carrier,RequestTrackerId,Message,Formatters,ContentTypes,DeclaredType,StatusCode的所有字段

1 个答案:

答案 0 :(得分:0)

不必在此处应用正则表达式,但如果需要,可以使用类似于以下内容的表达式:

"(FieldType)":"([^"]+)"|"(message)":"([^"]+)"

可能会提取那些所需的键值。

DEMO