bartonm
bartonm

Reputation: 1650

regex search and replace left and right boundary

For the given string,

I can haz haz bacon. Mmmm. Tasty, tasty bacon.
       ↑                            ↑

I want to capture content bounded by the first string "az" and the last string "as".

In this example I cannot use ^$ anchors. I tried using:

\b(az).*(as)\b.

What am I missing?

Upvotes: 3

Views: 1235

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250991

This would find the longest possible match:

(?<=az).*(?=as)

Demo: http://rubular.com/r/wzPdbrmbov

For shortest match:

(?<=az).*?(?=as)

Demo: http://rubular.com/r/GoPgYpGyI7

Upvotes: 3

Related Questions