1

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.

Link to Regex

My Regex

Italo Brenner
  • 128
  • 1
  • 7

4 Answers4

4

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.

Chris Belyeu
  • 208
  • 1
  • 9
bobble bubble
  • 16,888
  • 3
  • 27
  • 46
0

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

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
0

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)

Chris Belyeu
  • 208
  • 1
  • 9
0

A bit contrived but this could work:

(?<=INICIO).*?(INICIO.*?FIM)|(?<=FIM).*?(INICIO.*?FIM)

Your result will be in $2

https://regex101.com/r/qUf4kE/1

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77