user1015214
user1015214

Reputation: 3081

How to limit regex from grabbing more than one expression

Here is my regex:(<ImageContainer)([\s\S]+)([\s\S]+)(<\/ImageContainer>)

Here is some text I'm grabbing:

<ImageContainer>some stuff here...<\ImageContainer>more stuff here..<ImageContainer>and even more stuff</ImageContainer>...

My regex is grabbing the whole text and I only want it to grab from <ImageContainer> to </ImageContainer>. What am I missing?

Upvotes: 0

Views: 33

Answers (1)

Scott Weaver
Scott Weaver

Reputation: 7361

easy, use a lazy quantifier:

<ImageContainer>(.*?)<\/ImageContainer>

the sequence .*? will eat only one character at a time, then try to let the rest of the pattern match. Contrast this with regular greedy .*, which will instantly race to the end of the string, and then backtrack to try and let the rest of the pattern match.

Upvotes: 2

Related Questions