Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i got some HTML content from a Exchange website and i Splite code from "USD -US Dollar" onwards to a String .Now i want to get the values of Exchange rates ( 126.0 , 125.2739 ,130.0 ) from below String to a separate variables.I used "Index of" method.but due to repeating of same tags in the HTML content(in the string) it is hard to extract above values.



IF can someone help me on this matter. that would be a great help for me. Thanks in Advance!!!what i need is to extract above numeric values(126.0 , 125.2739 ,130.0) under USD-US Dollar tag.

XML
<b>USD -US Dollar</B></td>
<td bgcolor='gold' align='right' width='15%'>126.0</td>
<td bgcolor='gold' align='right' width='20%'>125.2739</td>
<td bgcolor='gold' align='right' width='20%'>130.0</td>
</tr>
</body>
</table>
</html>
Posted
Updated 15-Apr-12 4:21am
v4

Use the following Regex:
C#
public static Regex regex = new Regex(
    @"\<td.*\>(?<num>.*)\<\/td\>",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

Which will give you named matches called num.
 
Share this answer
 
Comments
Hesha 15-Apr-12 11:13am    
Hey Thanks a Lot !!!that's works Nicely. But little problem i got. when code executes it return only first value and rest of values are not returned.

code i used is as follows :
{
comp = s;
}

Regex regex = new Regex(
@"\<td.*\>(?<num>.*)\<\/td\>",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);

Match match = regex.Match(comp);
if (match.Success)
{
string v = match.Groups[1].Value;

string r = match.Groups[2].Value;

string s = match.Groups[3].Value;
TextBox1.Text = v;
TextBox2.Text = r;
TextBox3.Text = s;



}
can you give me a suggestion for that prob.then that will be a great help for me.thanks !!!
Mehdi Gholam 15-Apr-12 11:19am    
Use Matches : http://www.dotnetperls.com/regex-matches
Hesha 15-Apr-12 12:11pm    
Thanks a lot Mehdi!!! Now it's works perfectly.again Thanks a Lot!!!
Mehdi Gholam 15-Apr-12 13:53pm    
Great!
VJ Reddy 15-Apr-12 12:27pm    
Nice answer. 5!
The Solution 1 given by Mehdi Gholam is excellent. Only thing is that .* given in the regex captures everything including < and > so that we get only the last number i.e. 130.0. So, .* has to be replaced by [^<>]*.
Further, after capturing all the numbers with matches, the captured number will be Groups[1] in each match. So, Groups[2], Groups[3] stated by OP in the comment above, may not give the correct result.
Hence, the following code can be used to set the values as required.
C#
Regex regex = new Regex(@"\<td[^<>]*\>([^<>]*)\<\/td\>",RegexOptions.IgnoreCase | RegexOptions.Multiline);
var matches = regex.Matches(comp);
if (matches.Count==3){
    string v=matches[0].Groups[1].Value;
    string r=matches[1].Groups[1].Value;
    string s=matches[2].Groups[1].Value;
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Apr-12 22:46pm    
Good points and solution, a 5.
--SA
VJ Reddy 16-Apr-12 23:43pm    
Thank you very much, SA.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900