[PHP] Re: regular expressions help please

2002-04-30 Thread liljim

Hi Ed,

first example has been covered by others, so onto the next:

> Pulling a word between two other words without having to know what the
word
> is.  For example, pulling whatever word displays between "the" and
> "sky".  If the string was "the blue sky", the result would be the word
> blue.  If the string were "the green sky", the result would be the word
green.

I don't quite understand what you mean by "pulling", but this example will:
1. Take out any words if they exist.
2. Store those removed words into an array for you to use later, should you
need to see what was taken out.

Example 2\nOriginal string: " . $string . "\n";
if (preg_match_all("!the\s(.*?)\ssky!is", $string, $output))
{
 // Use an array to store all the matches.
 $saved = array();

 for($i=0; $iReplaced: " . $string . "\n\n";

if (!is_array($saved))
{
 echo $saved;
}
else
{
 echo "The following words were taken out of the pattern:\n";
 for ($i=0; $i\n";
 }
}

?>

There's nothing complicated about the match:
preg_match_all() - see manual
! delimiters !
the - first word
\s - space
(.*?) - everything up to next (non greedy)
\s - space
sky - word
i - case insensitive
s -  treat new lines (\n as .)

If you just want to rip stuff out, then you can use preg_replace():

$string = preg_replace("!(the)\s.*?(\ssky)!is", "$1$3", $string);

See the manual, or search these archives for further examples if you're
struggling Regexes (and when to use them) take a little getting used to,
 but they're definitely worth learning :)

Hope it helps.

James



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Re: regular expressions help please

2002-04-30 Thread CC Zona

In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Ed Lazor) wrote:

> I've been banging my head against regular expressions all night... help 
> would be greatly appreciated.  Could you give me examples on how to do the 
> following?
> 
> Pull everything except a specific word from a sentence.


First you need to decide what you mean by "word".  If it's simply "the 
stuff separated by a space", then you might find it simpler to explode your 
sentences on the space character, then loop through the resulting array 
using string functions to do your comparisons .  Regex is great for the 
power and flexibility it offers, but if you're really struggling with the 
syntax, using constructs that are more familiar may help you move forward.

On the other hand, it's frequently the case that one needs a more 
sophisticated definition of "word".  Is it just adjacent letters?  What if 
they're interrupted by a hyphen?  Are leading/trailing punctuation marks 
included?  Do adjacent digits count as words?   Etc. Once you've defined 
the pattern comprising a "word", you can then translate it into regex--or 
get more help from others in making the translation.

-- 
CC

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php