0

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.

3
  • 1
    You should explain exactly what you mean by: "it's not working". In addition, specify what you're trying to extract. If you're just looking for that exact string then you can do a hard match for that string. If you can provide 2-3 examples (they don't need to be the full text string) then you'll find people can give much better answers. Commented Sep 21, 2017 at 21:41
  • That looks like some kind of serialized data structure, similar to JSON. Maybe regexes are not the best tool to decode it. Where does this string come from? What do you intend to do with the cable { ... } result? Commented Sep 21, 2017 at 21:43
  • One reason why your regex attempt doesn't work is that the pattern has cable: but the string has cable without :. Commented Sep 21, 2017 at 21:49

1 Answer 1

2

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
)
Sign up to request clarification or add additional context in comments.

7 Comments

Good guess for what OP is looking for, but super hard to be certain.
@SlaterTyranus Agreed... I was itching to flex my fingers :-)
@SlaterTyranus Added another possible regex solution.
@cᴏʟᴅsᴘᴇᴇᴅ You should probably replace .*? with \s* at the beginning like so: cable\s*{.*?}\n? That way it doesn't catch something like cables {}
@ctwheels It's a non-greedy capture group, so that won't happen.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.