Re: Can this be RegEx, or do I have to go DOM?
am 29.09.2007 19:43:47 von klenwell
On Sep 29, 9:43 am, Good Man wrote:
> Hi there
>
> I have a series of HTML tables (well-formed, with elements ID'd quite
> nicely) and I need to extract the contents from certain TDs.
>
> For example, I'd like to get "Hi Mom!" from the example below:
> Hi Mom! |
>
> My RegEx skill leave much to be desired, I don't know how to capture data
> *between* two things (ie: the | )... can it be done? If
> so, can someone point me to how it can be done, or give me a big tip?
>
> If it can't be done, do I have to load the s as XML and go through
> the nodes searching for my content? That seems like a long-winded way to
> go, and though the table is well-formed, they are quite large and deep.
>
> There must be an easy RegEx solution if I always want to capture data
> between and ?
>
> Tips, guidance appreciated!
---
> There must be an easy RegEx solution if I always want to capture data
> between and ?
There is. In fact, it's the example used at the PHP preg_match_all
page:
http://www.php.net/manual/en/function.preg-match-all.php
To learn more about regex, see the PHP pattern syntax docs:
http://www.php.net/manual/en/reference.pcre.pattern.syntax.p hp
There are some helpful references in the user comments.
One additional bit of advice that might help. When trying to parse
data from a particular section of a large mass of tags like a web
page, I find it easier, if possible, to first isolate the section I'll
be focusing on by clipping at some consistent "landmarks". The
tag would be one example. This doesn't even require regex per se but
can use other PHP string functions like strpos and substr.
For example, say you want to parse the last result from a page of
google search results (http://lastgoogle.com/), you could look for a
unique constant marker at the bottom of the page like '
id=navbar', clip there, then use strrpos to backtrack from there to
another landmark to isolate the section you'll be parsing by regex.
Good defensive programming here also helps as stuff like this usually
requires some trial and error and it can be used to alert you in the
event any of the patterns you're expecting to be there unexpectedly
change.
Good luck,
Tom
Re: Can this be RegEx, or do I have to go DOM?
am 29.09.2007 19:46:09 von ragearc
There is of course a way to do it with Regex, but if your XHTML is
Valid, you can just use a XML parser and get all those items in a
simple function.