Capture first or second instance of match

The text looks like this:

I want to spread out the running over the week.{d}2020/01/01{d} {d}2020/02/02{d}

I am using this regex to capture the data within {d} markup.


However, this selects each group. How can I select only one (say first) instance of {d} match. That is, how do I only select the first {d}2020/01/01{d}.

And out of curiosity, how would that regex be different if I wanted to select the second match {d}2020/02/02{d}.


To match only first block just don’t use g flag:


To match last block use a greedy .* before it and capture it in group #1:


To get Nth match of this block:


Your match is in group #1.

Or else use /{d}[d/]+{d}/g for match and get Nth element from resulting array.

RegEx Demo