简体   繁体   中英

Regex: match text from multiple lines only if a specific condition is met, ignoring other text inbetween

I have the following text:

[Attribute Person]
name=John
description=This is a person
age=16

[Attribute Things]
name=Pencil\ndescription=This is an object

[Attribute Something]
name=John

[Attribute Animal]
name=Tom
description=Just a regular cat
age=2

I want to get the attribute name plus its description field ONLY if the attribute has a description field. From the text above I want to build a regex such that returns the following:

[Attribute Person] description=description=This is a person
[Attribute Things] description=description=This is an object
[Attribute Animal] description=Just a regular cat

Notice the attribute 'Something' is being ignored as it doesn't have the description field. Also, the description field of attribute 'Things' is inline with another field (note the \\n is intentional ).

So far I have:

(((^\\[Attribute \\w+\\])|((?<=\\n)\\[Attribute \\w+\\]))(?=[\\n.*]))[\\n\\w\\W]?|(description.*)

but I it is including the attribute without the description (attribute 'Something')

I am trying to do this in python | pcre

Looks like you need the ConfigParser

import configparser
config = configparser.RawConfigParser()
config.read('filename.ini')

print(config.get("Attribute Person", "description"))
print(config.get("Attribute Things", "description"))
print(config.get("Attribute Animal", "description"))

Output:

This is a person
This is an object
Just a regular cat

MoreInfo

You can do it this way if you want :

import re
string = """YOUR STRING GOES HERE"""
list_obj = re.find_all(r"Attribute(\s\w+)\]((\\n|\n)\w+\=\w+)?(\n|\\n)(description)\=(.+?)(\n|\\n)", string, flags = re.MULTILINE)
all_descriptions = filter(lambda x:x[5],list_obj)
print all_descriptions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM