Re: String /pattern formatting extraction question
| From: | Richard Lynch | Date: | Mon, 10 Apr 2006 19:48:27 +0000 |
| Subject: | Re: String /pattern formatting extraction question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-233694@lists.php.net to get a copy of this message | ||
On Mon, April 10, 2006 7:40 am, Andy wrote:
> Hi to all,
>
> I have the following pattern for a string:
>
> text1 /ptext2 /otext3
>
> Now, I want to extract the text by "patterns" from this string by the
> following rule:
>
> no pattern -> text1 (what is before /o or /p)
> /p -> text2 (what is after /p)
> /o -> text3 (what is after /o)
>
> The order betweeen /o and /p can be switched, so there is no rule that
> /p is before /o.
>
> How can I do this? I tried ereg but no success.
Will there always be at least one /p or /o?...
And I'm assuming you don't need the whitespace.
And that text2 and text3 must not have embedded whitespace.
Then you can do something like:
<?php
$pattern = '/^([^\\s])\\s(\\/(p|o)[^\\s])$/';
preg_match($pattern, $input, $output);
print_r($output);
?>
You may want to get a program called "The Regex Coach" which lets you
type patterns/input and see the output in real-time. Incredibly
handy/useful.
Also, your syntax looks AMAZINGLY like the GetOpt patterns of command
line arguments... If you could change '/' to '-', you'd find zillions
of pre-built libraries to handle your strings.
YMMV
--
Like Music?
http://l-i-e.com/artists.htm