I want to find a string that is between two specific words. See in the picture the pattern I made. The problem is that "INICIO" is appearing twice, I just want it to match it last appearance until the word "FIM". In this case, I wanted the first pattern to be INICIOBBBBBBBBBBBFIM. I need match all occurrence in the string.
-
Please put the examples in the question itself instead of relying on external sources. – kelvin Sep 19 '19 at 19:57
4 Answers
What you are looking for sounds like what Rexegg terms a tempered greedy token.
INICIO(?:(?!INICIO).)*?FIM
See your updated demo at regex101
The negative lookahead "tempers" the dot to not jump over another start. This practice is rather costly as before each position the lookahead looks ahead if there is no INICIO
ahead before proceeding.
If interested, there is a nice answer on SO explaining it in more detail.

- 208
- 1
- 9

- 16,888
- 3
- 27
- 46
Maybe,
.*(INICIO.*?FIM)
or
INICIO(?!.*\BINICIO\B)\B.*?\BFIM
might be OK to start with.
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
Demo 2
Reverse the string, and search for
MIF.*?OICINI
Then reverse the match
"JJJJJJJJJJJJJJJINICIOAAAAAAAAAAAAINICIOBBBBBBBBBBBBFIMJJJJJJJJJJJJJJINICIOJJJJJJJJJJJJJJFIM"
.split("")
.reverse()
.join("")
.match(/MIF.*?OICINI/g)[1]
.split("")
.reverse()
.join("");
(Notice, I'm actually grabbing the second match here. This is because the matches are in reverse order as well)

- 208
- 1
- 9
A bit contrived but this could work:
(?<=INICIO).*?(INICIO.*?FIM)|(?<=FIM).*?(INICIO.*?FIM)
Your result will be in $2

- 20,375
- 4
- 36
- 77