正则表达式在方括号[]中查找字符串 [英] regex to find string within square brackets []

查看:315
本文介绍了正则表达式在方括号[]中查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获下面html字符串中方括号内的文本. 但是我下面的正则表达式不会分别获得'image'和imagealt',而是返回'image]"alt =" [imagealt.如果我从字符串中取出alt ="[imagealt]",它会按照我的期望/期望返回.

I want to capture the text within the square brackets in the html string below. But the regex I have below doesn't get 'image' and imagealt' seperately but returns 'image]" alt="[imagealt' instead. If I take out the alt="[imagealt]" from the string it returns as I would expect/want.

$html = '<h2>[title]</h2>
<div class="content"><img src="[image]" alt="[imagealt]" /></div>
<div class="content">[text]</div>';

preg_match_all("^\[(.*)\]^",$html,$fields, PREG_PATTERN_ORDER);

echo "<pre>";
print_r($fields);
echo "</pre>";


Array
(
    [0] => Array
        (
            [0] => [title]
            [1] => [image]" alt="[imagealt]
            [2] => [text]
        )

    [1] => Array
        (
            [0] => title
            [1] => image]" alt="[imagealt
            [2] => text
        )

)

推荐答案

您的正则表达式很贪婪.您需要停止贪婪地做自己想做的事.在此处中找到有关贪婪的更多信息.

your regex is being greedy. you need to stop it being greedy to do what you want. Find out a bit more about greediness here.

当匹配项贪婪时,它将忽略满足正则表达式的第一种情况,并会继续尝试匹配项,直到它消耗尽可能多的输入为止.

When a match is greedy it will ignore the first situation which satisfies the regex and will keep trying to match until it consumes as much of the input as it can.

通常,这涉及添加?,但是我不确定在php中,但是您可以尝试:

Usually this involves adding a ? but I'm not certain in php, but you could try:

preg_match_all("^\[(.*?)\]^",$html,$fields, PREG_PATTERN_ORDER);

这篇关于正则表达式在方括号[]中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆