I'm going to create a tool which displays webpage rank in VB.NET.
For that I use
Dim str As String = New WebClient().DownloadString(("http://www.alexa.com/siteinfo/" + TextBox1.Text))
And I just want the Global Rank of that url which I provided in textbox1.text
Like here, I provide example.com to check its Alexa global rank:
I just need to display the global ranking number in my VB form.
Plz take a look at this:
Sub Main
Dim str As String = New WebClient().DownloadString(("http://www.alexa.com/siteinfo/example.com"))
Dim pattern = "a href=""/siteowners/certify.+?\>(?<rank>[0-9,]+?)\<\/a\>"
Dim r = new Regex(pattern, RegexOptions.IgnoreCase)
Dim m As Match = r.Match(str)
If m.Success Then
Debug.Print("Global rank " + m.Groups(1).ToString())
m = m.NextMatch()
Debug.Print("Usa rank " + m.Groups(1).ToString())
Else
Debug.Print("Failed")
End If
End Sub
On my computer answer is
Global rank 8,893
Usa rank 10,060
This code need better error handling but I guess it is ok as a proof of concept.
UPD. Some words on how it works:
The code above uses regular expressions (please take a look at this link to get started) to parse web page and extract the values you need.
On the screenshot you provided one can see that the ranks are stored in html <a> tag, which I identify by its href attribute, since it is the only <a> tag on the page, whose href attribute starts with string "/siteowners/certify". Hence, my regular expression matches for inner text of that tag and extracts it into match group.
Related
I need to produce code within Visual Basic that identify's a words position. For example, my sentence could write 'This is my Visual Basic Project'. If the user entered the word 'my', the output will open another form displaying 'Your word is in the 3rd position'. Its required to use strings then split it into an array, then using the match function give each word individual properties/positions.
I am fairly new to programming and would love any help. I would appreciate it if you could return some code for my design e.g buttons and listboxes. I have tried incredibly hard to get this program fully functioning but i'm finding it very challenging.
Really please. Many thanks!!
First of all, I am not a Visual Basic or .NET person, but I really liked the problem and so optimization of my code is possible . I am little confused by, what do you mean by match function. Are you looking for REGEX or something for string matching over here?
Anyways, based on your description, I tried to code something for you, which I think is something what you are looking for.
CODE:
The whole logic is within the click of the button "FIND POSITION OF WORD". Split the sentence then compare the entered word with each word in sentence
Public Class FindTheWord
Private Sub buttonFindTheWord_Click(sender As Object, e As EventArgs) Handles buttonFindTheWord.Click
Dim inputSentence As String = TextBox1.Text
Dim inputWord As String = TextBox2.Text
Dim SplittedSentence As String() = inputSentence.Split(" ")
Dim Position As Integer = 0
For Each word In SplittedSentence
Position = Position + 1
If (word = inputWord) Then
MessageBox.Show("Your word is at position : " + Position.ToString)
End If
Next
End Sub End Class
Hope this helps.
I want to get specific characters from listbox, but I don't know how to do it properly. I already used search (tried because I don't know how properly to name) but get nothing.
So i have this line in my listbox:
1,2014-01-01,Text,Text,XYZ123,Text,Text
How do i need to get only XYZ123? Its always same format, 3 letters and 3 numbers.
Thank you.
I would use a Regular Expression
The Regex of XYZ123 = \w{3}\d{3}
First solution:
Based on a small console application:
Dim i As String = "1,2014-01-01,Text,Text,**XYZ123**,Text,Text"
For Each Str As String In i.Split(",")
Dim match As Match = Regex.Match(Str, "\w{3}\d{3}")
If match.Success Then
Console.WriteLine(Str)
End If
Next
Console.ReadLine()
Second (better) solution:
Based on the comment of Chinz (all credits belong to him)
Dim i As String = "1,2014-01-01,Text,Text,**XYZ123**,Text,Text"
Console.WriteLine(Regex.Match(i, "\w{3}\d{3}").Value)
Console.ReadLine()
if all the strings have the same overall format you could split on "**" and get the [1] from the plitted
I'm trying to accomplish something fairly simple in VB that I do everyday in JavaScript.
I need to parse text between two strings (HTML tags mainly) that have multiple occurrences.
Sample Data:
<tag>test</tag>
<tag>test2</tag>
<tag>test3</tag>
If I wanted to grab the data in the 2nd <tag> in JavaScript I would simply do this:
var result = string.split('<tag>')[2].split('</tag>')[0];
And the only way I seem to get that to work in VB looks like this...
Dim from = string.IndexOf("<tag>")
Dim [to] = string.IndexOf("</tag>", from)
Dim result = string.Substring(from + "<tag>".Length, [to] - from - "<tag>".Length)
Mind you that is only the first occurrence in VB and already the code looks ridiculous in comparison... I didn't even want to figure out the 2nd occurrence until I find out this is my only solution. Thanks
You can do about the same thing in VB using the 'Split' method on the String.
Dim sx As String = "<tag>test</tag> <tag>test2</tag> <tag>test3</tag> "
Dim sp As String = sx.Split(New [String]() {"<tag>"}, StringSplitOptions.RemoveEmptyEntries)(1).Split(New [String]() {"</tag>"}, StringSplitOptions.RemoveEmptyEntries)(0)
My coworker needs me to write him a regular expression for his vb.net app.
I do not know vb and he does not know regex.
The regex he needs is:
/.*web id: ?(\d+).*/i
Basically he needs to search a string for something like "web id: 345" or "web id:2534" and retrieve the ID.
He took what I gave him above and was able to put this together:
Dim strPattern As String = ".*web id: ?(\d+).*"
Dim strReplacement$ = "$1"
GetWebId$ = Regex.Replace(LCase$(strNote$), strPattern$, strReplacement$)
However I am not sure how you pass the case-insensitive flag? (his current fix for that is making the whole string lowercase first)
Also one thing I can't seem to figure out is when he runs this on a string with multiple lines, any text that is not on the same line as "web id: \d" is also being returned which i find strange.
Use the RegexOptions.IgnoreCase flag:
Regex.Replace(strNote, strPattern, strReplacement, RegexOptions.IgnoreCase)
If you are going to ignore case there should be no need to use LCase. I also find it odd that you have all those $ symbols in your variable names - they shouldn't be valid in either C# or VB.NET.
EDIT #2: I realize you may have wanted to replace the entire line that matched with the $1 replacement pattern to match the ID. If you have a need to use multiple options you can Or them together as follows:
Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
EDIT #1: you are using the wrong method to extract the ID. You have a group (\d+) to capture the ID, but you are using Regex.Replace on your match, which is why you get everything else in the text. To match the ID use the following:
Dim input As String = "foo web id:2010 bar"
Dim pattern As String = ".*web id: ?(\d+).*"
Dim m As Match = Regex.Match(input, pattern, RegexOptions.IgnoreCase)
If m.Success Then
Dim id As String = m.Groups(1).Value
Console.WriteLine("ID: " & id)
Else
Console.WriteLine("No Match!")
End If
You will notice we refer to Groups(1) which holds the value captured by the (\d+) group. Patterns with more groups may lead to confusion, especially with nested groups. In those cases you can use named groups. Here is the same code updated to use named groups:
Dim input As String = "foo web id:2010 bar"
Dim pattern As String = ".*web id: ?(?<ID>\d+).*" ' group name added '
Dim m As Match = Regex.Match(input, pattern, RegexOptions.IgnoreCase)
If m.Success Then
' refer to group by group name '
Dim id As String = m.Groups("ID").Value
Console.WriteLine("ID: " & id)
Else
Console.WriteLine("No Match!")
End If
Kind of unrelated, but this code is a collection of things you shouldn’t do in VB.NET.
You shouldn’t use the old $ suffix on string variables, and likewise you shouldn’t use old functions such as LCase$. There are equivalent functions in the framework that should be used. You can also tell your friend to always enable Option Strict while where at it. This will catch a lot of potential bugs.
Furthermore, to set the return value of a function, the … “more established” method is to use Return …, not Functionname = ….
So the “correct” code will look like this:
''// I’m assuming that `GetWebId` is the name of the function we’re in.
Function GetWebId(note As String) As String
Dim pattern As String = ".*web id: ?(\d+).*"
Dim replacement As String = "$1"
Return Regex.Replace(note.ToLower(), pattern, replacement)
End Function
See Ahmad’s solution of how to get the “ignorecase” flag into the expression.
I'm working on building an HTTPHandler that will serve up plain text for use with jQuery Autocomplete. I have it working now except for when I insert the first bit of text it does not take me to the right portion of the alphabet.
Example: If I enter Ne
my drop down returns
Nlabama
Arkansas
Notice the "N" from Ne and the "labama" from "Alabama"
As I type the third character New, then the jQuery returns the "N" section of the results.
My current code looks like this
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
''# the page ContentType is plain text
HttpContext.Current.Response.ContentType = "text/plain"
''# store the querystring as a variable'
Dim qs As Nullable(Of Integer) = Integer.TryParse(HttpContext.Current.Request.QueryString("ID"), Nothing)
''# use the RegionsDataContext'
Using RegionDC As New DAL.RegionsDataContext
''# create a (q)uery variable'
Dim q As Object
''# if the querystring PID is not blank'
''# then we want to return results based on the PID'
If Not qs Is Nothing Then
''# that fit within the Parent ID'
q = (From r In RegionDC.bt_Regions _
Where r.PID = qs _
Select r.Region).ToArray
''# now we loop through the array'
''# and write out the ressults'
For Each item In q
HttpContext.Current.Response.Write(item & vbCrLf)
Next
End If
End Using
End Sub
So where I'm at now is the fact that I stumbled on the "Part" portion of the Autocomplete method whereby I should only return information that is contained within the Part.
My question is, how would I implement this concept into my HTTPHandler without doing a fresh SQLQuery on every character change? IE: I do the SQL Query on the QueryString("ID"), and then on every subsequent load of the same ID, we just filter down the "Part".
http://www.example.com/ReturnRegions.axd?ID=[someID]&Part=[string]
Just grab the part from the querystring and use it in the Where clause to filter only Regions that start with it AND have the appropriate ID.
ToArray is not necessary
I changed my jQuery library to use the jQuery-UI and then built my iHttpHandler using a Singleton object in order to avoid hitting the database on every page load. The answer here seems to be working well for me.