Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

VB.NET Regex <span class=bld>\s*(.+?)\s*</span>

Troubleshooting a piece of code.
            Dim web As Net.WebClient = New Net.WebClient()
            Dim html As String = web.DownloadString("https://www.google.com/finance/converter?a=1&from=EUR&to=USD")
            Dim m1 As Match = Regex.Match(html, "<span class=bld>\s*(.+?)\s*</span>", RegexOptions.Singleline)
            Dim currency As String = m1.Groups(1).Value.Replace("USD", "")

            user.setEuro(Convert.ToDouble(currency))

Open in new window

currency is blank.  

My question is what does "<span class=bld>\s*(.+?)\s*</span>", RegexOptions.Singleline" do?
Avatar of Rgonzo1971
Rgonzo1971

HI

This regular expression try to find then html element enclosed in
<span class=bld>

Open in new window

and
</span>

Open in new window

but it seems wrong
try instead
<span class=bld>\s*(.+?)\s*<\/span>

Open in new window

refer to
https://regex101.com/r/E2Z4hX/1

m1.Groups(1) 

Open in new window

is the part in parenthesis - (.+?) - to be changed where "USD" is replaced with nothing("")

Regards
Avatar of CipherIS

ASKER

Little confused.  You suggested to use <span class=bld>\s*(.+?)\s*</span>.  But that is exactly what is being used in the Regex.
            
Dim m1 As Match = Regex.Match(html, "<span class=bld>\s*(.+?)\s*</span>", RegexOptions.Singleline)

Open in new window

No at the end the / is escaped by a \
<\/span>

Open in new window

I am not sure if regex in .net "/" needs to be escaped
Ah.  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi CipherIS;

The reason why you are getting the "currency is blank" is that there is no tag in the html that has this in it, <span class=bld>, therefore the variable m1 is empty.

If this was working before maybe they changed the format of the html?