Snowcrash
Snowcrash

Reputation: 86097

Regex to capture TEST[some text] separately

Struggling to create the regex to capture this:

TEST[some text] 

and these:

TEST[some text]    |    TEST[some text]     |    TEST[some text]

but to capture the last group as 3 separate items.

I got as far as /([A-Z]+\[.+\])/g but that captures the line above as 1 item.

It also doesn't capture nested items like:

A[
  B[text]
  More text.
  C[and some more text]
] 

Effectively I want to capture A (and its associated values within the brackets), B (and its values), More text., and C (and its values) separately.

Basically the structure goes like:

KEY[VALUE]

and VALUES can consist of more KEY/VALUE pairs.

Perhaps there's some more generic way of creating / parsing these text structures (i.e. like a YAML file) that I'm not aware of. Any suggestions?

Sorry for the rather rambling and open-ended question.

Upvotes: 0

Views: 37

Answers (1)

Emil Ingerslev
Emil Ingerslev

Reputation: 4765

Indeed there is flavors to take into consideration, but what you're looking for can be done without too much flavor specific stuff

/([A-Z]+\[[^\]]+\])/g

Since [^\]]+ will match any non-"]" character, you won't get nested ones. BUT, it wont match the A one from your examples, but I guess thats not needed either?

Upvotes: 1

Related Questions