Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to find string within square brackets []

Tags:

regex

php

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
        )

)
like image 869
user813720 Avatar asked Mar 12 '12 10:03

user813720


People also ask

What do the [] brackets mean in regular expressions?

Square brackets ( “[ ]” ): Any expression within square brackets [ ] is a character set; if any one of the characters matches the search string, the regex will pass the test return true.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .

How do you match square brackets in regex?

[[\]] will match either bracket. In some regex dialects (e.g. grep) you can omit the backslash before the ] if you place it immediately after the [ (because an empty character class would never be useful): [][] .

Can a regex identify correct bracketing?

Regular expressions can't count brackets.


1 Answers

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.

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

preg_match_all("^\[(.*?)\]^",$html,$fields, PREG_PATTERN_ORDER);
like image 126
Sam Holder Avatar answered Sep 23 '22 23:09

Sam Holder