1

I have this text, which is used for pagination and comes as a string:

[https://example.com/page2]; rel="prev", [https://example.com/page4]; rel="next", [https://example.com/page1]; rel="first", [https://example.com/page7]; rel="last"

I need to extract from this string the following sub-string, that is the next link on the pagination, as informed in the "rel" attribute right after it:

https://example.com/page4

What I tried to do to achieve this is to match everything exactly before this text: ; rel="next", that is between brackets. So I came up with this regular expression:

(\[(.*?)\]); rel="next"

The problem here is that it considers the closing bracket that is right before ; rel="next", but for the opening bracket it gets the first one, which means the result is as follows:

https://example.com/page2]; rel="prev", [https://example.com/page4

I have to look for ; rel="next" because the order of the elements can't be trusted, they might come sorted differently or some of them might not come at all.

Is there a way to indicate in my regex the correct opening bracket? or maybe there is a better way to get the same result?

Carlos
  • 13
  • 5

0 Answers0