I have two specific words in a line say dog
and cat
.
And say dog
appears before cat
.
How do I make a regex which tells me all the lines containing dog
and cat
, but show just the words after dog
till cat
, including cat
, but NOT dog
.
Option 1: Lookbehind
(?<=dog).*?cat
Details:
(?<=dog)
lookbehind asserts that what precedes the current position is dog
.*?
lazily matches all characters up to...cat
matches literal charactersOption 2: \K
(PCRE, Perl, Ruby 2+)
dog\K.*?cat
The \K
tells the engine to drop what was matched so far from the final
Option 3: Capture Group (JavaScript and other engines that don't support the other two options)
dog(.*?cat)
The parentheses capture what you want to Group 1. You have to retrieve the match from Group 1 in your language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With