Jump to content

Match all characters but a word


freshwebs

Recommended Posts

How do I match all characters except for a certain word?  I need to match text which may contain line breaks but stop when it comes to a </div> tag.  I've just been using "<div>([[:print:][:space:]]*?)</div>" but I feel like there's a better way to do it.

Thanks for any help!

Aaron

Link to comment
Share on other sites

That's it right there, this is a great use of lazy quantifiers (the *? in this example). You could get fancy (read: crazy) and do something like:
[code]preg_match_all('/<div>([^<]*(?:<(?!\/div)|[^<]*)*)<\/div>/is', $html, $matches);[/code]
But at some point you're really just beating a dead horse. Which way is easier? To me, hands-down your way (I just cooked this one up as a "devil's advocate" counter example, plus regex is FUN!). When I scrape things, I do it the way you do:
[code]preg_match_all(/'<start tag>.*?<\/end tag>/s', $html, $matches);[/code]
As long as it matches the right stuff in polynomial time, I'd say that's a pretty good way to do it.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.