Search code examples
regex-group

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:

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.