简体   繁体   中英

How to create regex for the expression starts with and ends with curly braces?

I want to find a string in a text file which starts with '{"ABC":' and ends with '}' .

So far I have tried following:

re.findall('^{\"ABC(.)*$}', text_string, re.MULTILINE)

But it's not working as expected.

If you want to capture the whole string:

re.findall('({"ABC":.*})', text_string, re.MULTILINE)

If you want to capture just the contents:

re.findall('{"ABC":(.*)}', text_string, re.MULTILINE)

Of course, this can be useful to extract JSON-like structures from plain text data, but do not use regex for parsing any sort of structured/multi-level data - use a parser created for it, like the built-in module json for JSON.

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