Question RegularExpression for html?

Spelltox

Member
Joined
Feb 27, 2009
Messages
8
Programming Experience
3-5
Hi to all !

I am new to "Regular Expressions",
tried to figure out all these weird stuff but I can't seem to get it right ...

I need to extract only the "555" out of an full html string (of course it could be any other number).

part of the html string :

</div>
<div class="partWeight">
it weighs:<br />
<span>555 KG</span>
</div>
<div class="partSize">

the web page contain several parts like this,
so I need to go through all the matches,
I'm using VB.NET, so please if you can help with a piece of code that would be even better,
because I'm not sure if my regEx is wrong or the vb.net implementation ...
(C# is also an option).

Thanks in advance guys !
 
Hey Spelltox...I was just getting ready to post my question on RegExpressions because I am also new but here is what I have to grab code from a website...doesn't quite work YET because you need to actually setup the regular expression correctly but maybe this will give you a template to start with...took me a freaking day to get up to this part. :(...

Just pop a button and a rich text box on your screen.

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] Imports[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Net[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.IO[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Text[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Text.RegularExpressions[/COLOR]
[COLOR=#000000][/COLOR]
[COLOR=#000000][/COLOR]
[COLOR=#000000][/COLOR]
[COLOR=#000000][/COLOR]
[COLOR=#000000][/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] Form1[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button1.Click
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] net [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Net.WebClient()
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] src [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]src = net.DownloadString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"http://www.wikihow.com/Make-Easy-Homemade-Biscuits"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' Create a match using regular exp<B></B>ressions
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] m [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Match = Regex.Match(src, [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"src="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' Spit out the value plucked from the code
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]RichTextBox1.Text = m.Value
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub
End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE][/COLOR][/SIZE]

Where it reads "src, "src="" you will need to find your regex you are looking for...best of luck...also hopefully someone will anwser my thread I am about to post on how to get the "src" of an IMG on a web page...maybe you can use that for something as well. :calm:
 
If you're looking for "555 KG" that is simple enough, the pattern is repeated numbers followed by string " KG". If that is not it you should explain better.
 
Back
Top