Matt Elhotiby
Matt Elhotiby

Reputation: 44066

Regex in between last occurrence text

I have this test string

[list][*] This is [bracket text][/list]

I want to grab all the [*] and after up until the last [

I am using this regex \[\*(:[^\[]+)?\]([^\[]+) which only grabs till the first [ and leaving off the [bracket text] part of the string

I need the match to be This is [bracket text]

Any thoughts on how to achieve this

Upvotes: 0

Views: 48

Answers (1)

anubhava
anubhava

Reputation: 784998

You can use this greedy regex with a lookahead:

\[\*\] *(.*)(?=\[)

.* is greedy to make sure to match until assertion of last [ using lookahead.

RegEx Demo

Upvotes: 2

Related Questions