Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find multiline text between curly braces?

Tags:

python

regex

I have next input string:

ANIM "_NAME_KEY_" // Index = 26, AFrames = 1
    {
        0x301C
        AF  0x201C  1   0   0   FREE_ROTATE 0   FREE_SCALE_XY 100 100
    }

How can i get whole string between two curly braces, just having _NAME_KEY_ ?

like image 229
askona Avatar asked Aug 19 '13 15:08

askona


People also ask

What are the code between the curly braces?

Blocks: The code between a pair of curly braces within any method or class is called as blocks.

How do you match curly brackets in regex?

To match literal curly braces, you have to escape them with \ . However, Apex Code uses \ as an escape, too, so you have to "escape the escape". You'll need to do this almost every time you want to use any sort of special characters in your regexp literally, which will happen more frequently than not.

What are the curly brackets {{ }} used for?

In writing, curly brackets or braces are used to indicate that certain words and/or sentences should be looked at as a group. Here is an example: Hello, please pick your pizza toppings {chicken, tomatoes, bacon, sausage, onion, pepper, olives} and then follow me.

How do you escape curly braces in F string?

Format strings contain “replacement fields” surrounded by curly braces {} . Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }} .


2 Answers

Use the option re.MULTILINE as a second argument to your re.compile/etc. call.

I would propose this regex: _NAME_KEY_[^{]*+\{([^}]+)\}

Explanation:

_NAME_KEY_: match "_NAME_KEY_"

[^{]*: match as many non-{-characters as possible (greedy)

\{: match a { character

([^}]+): match (and capture) non-}-characters (greedy)

\}: match one } character

like image 166
L3viathan Avatar answered Oct 03 '22 19:10

L3viathan


re.findall(r'(?<={)[^}]*',str)

E.g.

In [5]: x="""ANIM "_NAME_KEY_" // Index = 26, AFrames = 1
    {
        0x301C
        AF  0x201C  1   0   0   FREE_ROTATE 0   FREE_SCALE_XY 100 100
    }"""

In [6]: import re

In [7]: re.findall(r'(?<={)[^}]*',x)
Out[7]: ['\n        0x301C\n        AF  0x201C  1   0   0   FREE_ROTATE 0   FREE_SCALE_XY 100 100\n    ']
like image 42
Kent Avatar answered Oct 03 '22 18:10

Kent