duvo
duvo

Reputation: 1734

Regular expression negative look ahead

Not too familiar at RE. I have the following string

"The cat jump The cat ran The cat eat The dog jump The dog ran The dog eat".  

I want to get

"The cat ran" and "The dog ran".  

I thought this RE using negative look ahead should work

"The(?!.*The).*jump" 

but it's not. What is missing?

Please help.

Upvotes: 1

Views: 108

Answers (2)

Qtax
Qtax

Reputation: 33908

Seems that you want an expression that matches a substring that does not contain The. That can be done with a construct like (?:(?!The).)*, in your expression:

The(?:(?!The).)*jump

Note that in this case using lazy (.*?) quantifiers would suffice, eg:

The.*?jump

Maybe that can work on your original problem too.

Upvotes: 1

Wug
Wug

Reputation: 13196

Would the following do it for you?

The \w* ran

Upvotes: 1

Related Questions