-3

I am new to make regex and I want to know how to make a perfect regex that can do exactly its job.

This is an example:

I want to extract all elements that match this one from HTML document

<span class="st">
    <em>Echo</em> Reservoir, <em>UT</em>, 
    local weather including current conditions, extended forecasts,
    alerts, <em>Utah</em> weather map and more.
</span>

and want to bring text only sometimes. Is it possible? Thanks.

Dean Ward
  • 4,793
  • 1
  • 29
  • 36
Mohamed Hana
  • 63
  • 1
  • 2
  • 11

2 Answers2

0

There are many reasons not to use regex to parse and extract things from HTML but I'll leave it to bobince to explain them: RegEx match open tags except XHTML self-contained tags

It would be better to use a dedicated HTML parser to perform the task. A quick Google search suggests PHP HTML Parser for PHP which also includes several examples.

If you're using VB.NET then use HtmlAgilityPack.

Community
  • 1
  • 1
Dean Ward
  • 4,793
  • 1
  • 29
  • 36
0

I have used Simple HTML DOM on numerous PHP projects. It will do exactly what you want.

http://simplehtmldom.sourceforge.net

ryantxr
  • 4,119
  • 1
  • 11
  • 25