Regex - Recovering Property from Pre-formatted File

Asked

Viewed 37 times

1

The idea is to develop a tool that selects fields from a pre-formatted file according to a specific schema:

Schema:

1.   
[Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris efficitur 
laoreet gravida. Nunc lacinia elit in nulla ultrices viverra eget eget 
massa. Phasellus suscipit pharetra ex non euismod. 
Sed mollis laoreet odio id blandit. Maecenas vel orci sed lacus commodo 
commodo et a mauris.


{asd123ASD/asd123ASD/a1A/a1A/a1A -:_ }

]

Basically a text file with an index and a content between the [ ] which highlight an unbounded string and a command block bounded by { }.

The goal is to capture the data in groups. Ex.: Indexes, Text Commands.

I saw that an "easy" way to solve is by using RegEx.

So I came to this:

(\d{1}[[:punct:]]\s*\[){1}(.*\s*\{)(.*\s*\])

https://regex101.com/r/Md4L6b/1

But this returning only 2 match where there should be 5.

Can someone tell me where I’m going wrong?

1 answer

2


The problem is that some indices are different from the first and the fourth. Indexes 2,3 and 5 have spaces between text, which regex cannot match

Down with the regex you can catch

(\d[[:punct:]])\s*\[([a-zA-Z\s[[:punct:]]*)\s*{(.*)}\s*\]

See working https://regex101.com/r/Md4L6b/2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.