如何限制正则表达式抓取多个表达式

时间:2016-04-15 00:41:17

标签: regex

这是我的正则表达式:(<ImageContainer)([\s\S]+)([\s\S]+)(<\/ImageContainer>)

以下是我抓住的一些文字:

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

我的正则表达式正在抓取整个文本,我只希望它从<ImageContainer>抓取到</ImageContainer>。我错过了什么?

1 个答案:

答案 0 :(得分:2)

简单,use a lazy quantifier:

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

序列.*?一次只能吃一个字符,然后尝试让模式的其余部分匹配。将此与常规贪婪.*进行对比,它会立即竞争到字符串的末尾,然后回溯以尝试让模式的其余部分匹配。