fid200
fid200

Reputation: 47

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

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"?

Upvotes: 0

Views: 45

Answers (1)

Bohemian
Bohemian

Reputation: 425448

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.

Upvotes: 1

Related Questions