regex help

regex help

am 05.07.2005 15:50:56 von Sugapablo

Could someone help me out with a regular expression?

I need to be able to search a text field and grab every instance of a
string that falls between and .

We can assume that the tags will always be written properly.


So if the following is my text field: "They streamed across the Clemente
Bridge, docked along the Allegheny River, strolled
from the North Side and biked down the trail to watch their Pirates.

They filled PNC Park to the last sliver of
standing-room space on the rotunda, 37,259 strong.

And they came in full throat, chanting and cheering from the outset."

....I'd need the code to return, probably an array, like this:
$array[0] = Allegheny River
$array[1] = PNC Park
$array[2] = 37,259


I just can't get it. Any suggestions?

--
[ Sugapablo ]
[ http://www.sugapablo.net <--personal | http://www.sugapablo.com <--music ]
[ http://www.2ra.org <--political | http://www.subuse.net <--discuss ]

Re: regex help

am 05.07.2005 16:56:37 von Daniel Tryba

Sugapablo wrote:
> We can assume that the tags will always be written properly.

So why did you make an error in your example? :)

[string]
> ...I'd need the code to return, probably an array, like this:
> $array[0] = Allegheny River
> $array[1] = PNC Park
> $array[2] = 37,259
>
>
> I just can't get it. Any suggestions?

Tell us where you are running into problems. It's really simple if:
-you know the correct function
-you know your regexpes.

Do you want to learn how to do it or do you just want the answer of how
to get an array like:

Array
(
[0] => Array
(
[0] => Allegheny River
[1] => PNC Park
[2] => 37,259
)

[1] => Array
(
[0] => Allegheny River
[1] => PNC Park
[2] => 37,259
)
)

If you want to learn see
http://nl2.php.net/manual/en/function.preg-match-all.php (the examples
contain samples that match your question closely) and
http://nl2.php.net/manual/en/reference.pcre.pattern.syntax.p hp (study
greedy vs. non greedy)

The spoiler is below:






















preg_match_all('/(.*?)<\/tag>/',$str,$matches);

Re: Regex help

am 18.12.2007 16:32:15 von Moi

Thanks Rik, I guess that makes sense.
P

Re: Regex help

am 18.12.2007 20:38:15 von Michael Fesser

..oO(Patrick Drouin)

>Hello Michael,
>
>> Nope. What's returned is the entire matched string and all parenthesized
>> sub strings (if there are any), but not every single matching point from
>> during the execution.
>>
>> The above is exactly what you told preg_match() to return:
>
>Well OK, let me rephrase then, how can I tell PHP to match the
>substrings. In my mind, (a+) means a, aa, aaa, ... and not only the
>maximum string. I don't see how that behaviour is logical in any way.

That's how regular expressions work in general. The only thing that you
can control in many regex engines is whether the engine should stop the
matching process after it has found a minimum match (ungreedy) or if it
should continue until the maximum length (greedy), which is usually the
default.

Micha