|
Posted by John Dunlop on 11/03/05 21:32
Paul wrote:
> Does anyone know if it's possible to use regular expressions to grab
> all content between two words. For example....
>
> ------
> This is some example text to illustrate my problem and here's another
> my for you.
> ------
>
> I want to grab all text between the words 'some' and 'my' with the
> correct result being ' example text to illustrate '
depends what you mean by 'word', a term which has various meanings.
1 approach to match what's between what's sometimes called orthographic
words could be:
`\bsome\b(.*?)\bmy\b`
the same as Sandman's but with assertions that the sequences <some> and
<my> are in fact orthographic words, that is, at both ends of each are
"word" boundaries (as defined by PHP). without these assertions the
sequence <my> appearing before the word <my> would throw the match.
> The following doesn't work with either ereg or preg_match...
>
> some([^(my)]+)my
because it's a match for <some>, then one or more of any character except
'(', 'm', 'y', or ')' until the last occurrence of <my>.
character classes are not ordered, they're lists of individual characters.
--
Jock
Navigation:
[Reply to this message]
|