Regex needed to get text between to words when text starting match appears earlier

61 views Asked by At

I am quite unexperienced in using regex. I have a text like this:

This is a demo text I need a special part The text i want to get is here.<

The result i need is this : "text i want to get" .So i have to search backwards from "get" to "text" i guess.

My regex (?<=text\s).*(?<=\bget) is returning "I need a special part The text i want to get"

How can I modify this regex to work backwards from "get" to "text"?

1

There are 1 answers

0
Bohemian On BEST ANSWER

Use a negative lookahead to assert that "text" does not appear again after the starting "text" of the match.

Also, if you want to include "text" and "get", you must match them (rather than look around for them).

text(?!.*text).*\bget\b

See demo.