tags:

views:

189

answers:

9

Hi all;

<tr><TD><FONT size="2">My Value 1</FONT></TD></tr>

I need "My Value 1" Please Help Me. C# language

A: 

Try:

/<tr>\s*<td>\s*<font.*?>(.*?)<\/font>\s*<\/td>\s*<\/tr>/i

Used in PHP:

<?php

if(preg_match('/<tr>\s*<td>\s*<font.*?>(.*?)<\/font>\s*<\/td>\s*<\/tr>/i',
              '<tr><TD><FONT size="2">My Value 1</FONT></TD></tr>',$matches))
        echo $matches[1]; // prints My Value 1
?>
codaddict
Dont Work codaddict
Oraclee
@oraclee, how doesn't it work? What is it returning?
Lazarus
@Lazarus return null
Oraclee
@codaddict i need c# language :-)
Oraclee
A: 
function stripTags(markup){
  return markup.replace(/\s*<[^>]*?>\s*/gim,'');
}

This assumes all you really want is the inner text represented by "My Value 1" above.

Robusto
+3  A: 

You cannot properly parse HTML with regular expressions because regexps can't handle the nesting allowed by HTML. To do it properly. For that one line you show, you can use a regexp but you can't count on that line remaining identical so must use SAX/DOM for the task generally.

msw
But you can parse a fixed string that happens to be HTML with regular expressions. While there are lots of issues with doing this, they're problems the OP probably doesn't have.
Tom
@Tom - agreed. But although the OP doesn't have them today, he might tomorrow and won't be left wondering what happened, hopefully.
msw
+7  A: 

As HTML code is very "unpredictable" I would recommend using a HTML parsing kit. Which programming language do you use? In .NET I have used HTML Agility Pack with great success. In Java HTML Parser might be handy (though I have not worked with it yet).

spa
A: 

if you are using PHP, split on </FONT>

$string='<tr><TD><FONT size="2">My Value 1</FONT></TD></tr>';
$s = explode('</FONT>',$string);
foreach ($s as $v){
     if ( strpos($v,"<FONT") !==FALSE) {
        $t = explode(">",$v);
        print end($t)."\n";
    }

}

output

$ php test.php
My Value 1
ghostdog74
i need c# language :-)
Oraclee
A: 

in perl I would use

my $string='<tr><TD><FONT size="2">My Value 1</FONT></TD></tr>';
$string =~ m/(<.*?>)*([^<]*)(<.*?>)*/;
print $2;

to get the desired result. The last part is not strictly necessary,

(<.*?>)*([^<]*)

will work as well

Bas
A: 

If you want to get the contents within the tags I think the following Regexp is enough:

^<.*>([^<>]+)<.*>$

It will only work if there really is any data between the tags somewhere, otherwise it will give a no-match.

Anders Abel
i need only value "My Value 1"
Oraclee
+3  A: 

I think parsing HTML using Regexes is not a wise idea, as highlighted by spa. A classic previous answer to a similar question is http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

MPritch
+2  A: 

c# language

 string input = "<tr><TD><FONT size=\"2\">My Value 1</FONT></TD></tr>";
 string pattern = @"<[^>]*?>";
 string output = Regex.Replace(input, pattern, ""); //My Value 1

Just to remove all html tags.

ldp615
its work thank you
Oraclee
you are welcome.
ldp615