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 recursive capture into single group

This is a really simple task, I have a string:

s/test/foo/bar

I want one group to contain s/test/ and the rest to be contained in another group:

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

group 1: s/test
group 2: foo/bar

My expression is:

(/s/test/)([a-z]+/)

This will do half of the job but only captures ‘foo’ in group 2. How do I also capture ‘bar’. I tried:

(/s/test/)([a-z]+/)*

But I’m mis-understanding something because that captures too much of the full string. I’ve created a demo here:

https://regex101.com/r/9h9WQs/1

>Solution :

Make the / character part of the second group.
(\/s\/test\/)([a-z\/]+)
Now only characters [a-z] are matched.

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