Adrija
Adrija

Reputation: 99

Extracting a substring following a specific pattern using regex in python

I have a string:

lx-ss {\n    id 1;\n    type mx2090;\n    template 4-32g;\n}\nlaxmx2k02-ie {\n    id 2;\n    chss mx2010;\n    resource-plate 4co;\n}\ncable {\n    id 3;\n    chassis mx2010;\n    plate 2c;\n}\n

I need to extract cable {\n id 3;\n chassis mx2010;\n plate 2c;\n}\n from the above string.

The regex that I am following is:

[\n\r].*\ncable:\s*([^\n\r]*)

But it's not working.

Kindly help. Thanks in advance.

Upvotes: 0

Views: 199

Answers (1)

cs95
cs95

Reputation: 403198

Use re.search and extract your string.


Option 1

.*\n(.*{.*}\n)
m = re.search('.*\n(.*{.*}\n)', string, re.DOTALL | re.M)

print(m.group(1))    
'cable {\n    id 3;\n    chassis mx2010;\n    plate 2c;\n}\n'

Regex Details

.*       # greedy match (we don't capture this)
\n       # newline
(        # first capture group - capture everything inside this
    .*       
    {        # opening brace
    .*       # content within braces (greedy/non-greedy doesn't matter)
    }        # closing brace
    \n       
)     

Option 2
This one is a bit more flexible, with regards to the positioning of your substring.

(cable\s*{.*?}\n?)
m = re.search('(cable.*?{.*?}\n?)', string, re.DOTALL | re.M).group(1)
print(m.group(1))  
'cable {\n    id 3;\n    chassis mx2010;\n    plate 2c;\n}\n'

Regex Details

(            # first capture group
    cable        # match "cable"
    \s*          # match 0 or more whitespace char
    {            # opening brace 
    .*?          # non-greedy matchall
    }            # closing brace
    \n?          # optional newline
)

Upvotes: 2

Related Questions