regex - Regular expression to get last occurence until next char -


i trying work out regular expression , stuck - can help.

i have string:

"this string [flag no match] should match [flag this] dont want [no no]"

i want capture last occurance of flag enclosed in brackets.

tried this:

\[flag(?!.*\[flag)(.*)\] 

i there, matches last "]" in string. how can match next "]" following match.

want match this: "this"

get this: "this] dont want [no no"

hope can help.

thanks

make * non-greedy,

\[flag(?!.*\[flag)(.*?)\] 

or

\[flag(?!.*\[flag)\s*([^\]]*)\] 

demo


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -