Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex for detecting multiple strings within square brackets?

Given the following example, what would the proper regex be to detect each individual occurrences of the text between brackets?

It needs to account for variation in number of characters within each as well as numbers in a specific position. Below is the example string it needs to match.

As you can see the "message" part is always the same, however the number of digits/characters of the values that comes after "message" can vary.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[message:92:Classy][message:8:Phoenix][message:922:Testing][message:1092:Another]

I would like a regex pattern that can match each individual occurrence of the [] strings.

Thanks in advance to all the regex experts!

>Solution :

Assuming that the pattern is always [message:digit:string], you can use the following pattern:

/\[(message:\d+:(\w|\s)+)\]/g

This will match all the occurences individually, but it has the downside of working only with English alphabet.

You can also use the following:

/\[.*\]/g

This will match all the characters between [ and ], but it may result in multiple groups being matched at once.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading