Gleb
Gleb

Reputation: 176

Get all matches from text with regex

I Have text

Text example (only example, I can't show real text):

some words getServerResponse={go [for {a walk}] goodby }something hello world text={what[ i want {in curl} ]braces}, another string text={second[ {text what} i ]want number two}

In this example "text" - it is tag which I need to get. We can see that all information I need is contained in curl braces after "text=".

I try to use this template: text=(.*) and expect to get from my text two elements in list:

{what[ i want {in curl} ]braces}, another string text={second[ {text what} i ]want number two}
{second[ {text what} i ]want number two}

Doesn't matter that first element contains second part, because from first element I get only what is between the first open curl brace and curl brace that closes the first one:

what[ i want {in curl} ]braces

But this text=(.*) template gives me only one variant

{what[ i want {in curl} ]braces}, another string text={second[ {text what} i ]want number two}

How can I get all matches from my text as list elements?

Upvotes: 0

Views: 55

Answers (1)

Pubudu Sitinamaluwa
Pubudu Sitinamaluwa

Reputation: 978

Assuming that all braces will be in the given format, I came up with this regex.

(text=)(\{(\w|\[|\s|\{|\}|\])+)

This will capture the nested element after each text=. Note that I have added 3 matching groups. 3rd group can be ignored for now.

Group1 => text=

Group2 => {what[ i want {in curl} ]braces} and {second[ {text what} i ]want number two}

You can use match all and extract the second capturing group to get your desired result.

RegexDemo

Upvotes: 1

Related Questions