komodo
11-04-2006, 05:44 PM
Regex has always been a pain for me, but for some reason I'm having unusal difficulty with this one.
I need to find the entire content between either two double-quotes OR two single-quotes that contain a certain word inside a large document.
For example, if I have a paragraph like this:
Among dog lovers, "dogs are generally valued" for "their intelligence", and both "anecdotal evidence and scientific" research suggest that dogs have a reasonably high intelligence. "This intelligence is" expressed "differently with different breeds" and individuals, however.
And I run a command like this:
preg_match("/[\"|'](.+?and.+?)[\"|']/i",$paragraph,$matched);
I know that's not right, since the pipes are being interpreted as a literal because of the square brackets.
I'm trying to return this:
anecdotal evidence and scientific
The only quoted phrase that includes "and".
I figured out one way that works:
/(\"|')(.*and.*)(\"|')/i
Except, if the word I'm looking for is at the very end of the phrase, directly before the quote, it reads right over the quote and keeps reading all the way until it hits the next quote.
With the .* though, it matches zero characters, right? So it shouldn't matter if there's no characters between the word and the quote?
I'm still working on it, but I'd appreciate any help. Thanks.
I need to find the entire content between either two double-quotes OR two single-quotes that contain a certain word inside a large document.
For example, if I have a paragraph like this:
Among dog lovers, "dogs are generally valued" for "their intelligence", and both "anecdotal evidence and scientific" research suggest that dogs have a reasonably high intelligence. "This intelligence is" expressed "differently with different breeds" and individuals, however.
And I run a command like this:
preg_match("/[\"|'](.+?and.+?)[\"|']/i",$paragraph,$matched);
I know that's not right, since the pipes are being interpreted as a literal because of the square brackets.
I'm trying to return this:
anecdotal evidence and scientific
The only quoted phrase that includes "and".
I figured out one way that works:
/(\"|')(.*and.*)(\"|')/i
Except, if the word I'm looking for is at the very end of the phrase, directly before the quote, it reads right over the quote and keeps reading all the way until it hits the next quote.
With the .* though, it matches zero characters, right? So it shouldn't matter if there's no characters between the word and the quote?
I'm still working on it, but I'd appreciate any help. Thanks.