1

I've seen a bunch of examples on this online but I can't seem to find the one I'm looking for which uses Regex. I've seen many that use a loop and use a lot of lines of code but I'd like to see an example of Regex.

What I'm trying to create is an app that will connect too a webpage take the source search for a keyword once its found copy the text from that keyword to another keyword and save it into a string or to a textbox whatever.

I'm already using web request to get the information and put it into a string I just need to search the string for what I am looking for.

The reason for this app is to search webpage for an updated version of some software I'm using. I want to monitor for updates and the app to notify me when an update is available. Just a simple app but having issues searching for what I need.

For Example:
first words to search for: Server 64-bit
second words/characters to search for: </div>

grab first words everything in between and last word saved into a string.

EDIT: The information I am trying to grab is this....

Server 64-bit
                  <span class="version">
          3.0.13.6
                      </span>
              </h3>
                    <div class="checksum">SHA256: c7eeb1937b0bce0b99e7c7e20de030a4b71adcaf09750481801cfa361433522f</div>
  • Possible duplicate of [Extract specific html string from html source code(website) in vb.net](http://stackoverflow.com/questions/24755662/extract-specific-html-string-from-html-source-codewebsite-in-vb-net) – Fabio Apr 02 '17 at 16:14
  • How is that a possible solution if I have to download something in order to make this work? There has to be a way with the original vb .net to do this without having to add third party to it. –  Apr 02 '17 at 22:01

2 Answers2

0

Maybe not the prettiest solution, but i would save it into a string. Then iterate through it with the string.contain("Server 64-Bit") and then split the whole thing and then split the remaining part of the string at the next and retrieve only the first part.

Dim Information As String
Dim Splitstring As String
If Information.Contains("Server 64-Bit") Then
        Dim parts As String() = Information.Split("Server 64-Bit")
        For Each part In parts
           SplitString As String = part(1)               
        Next
   If SplitString.Contains("</div>") then
       Dim parts As String() = Information.Split("</div>")
       For Each part In parts
           Dim ResultString As String = part(0)
           'Displaying Result in a MsgBox
           MsgBox(ResultString) 
        Next
   End If
End If

Im currently only at my Phone, so I cant actually test this, but this should work.

Alex
  • 112
  • 10
  • 1
    IT won't work because you declared a string inside the for each and it can't be used in the next if statement. Will have to make the the SplitString public –  Apr 02 '17 at 19:28
  • for some reason neither your code or Hadi is working. I have edited my post to show the code I'm trying to pull from the html. Maybe that would help us figure out why. –  Apr 02 '17 at 21:13
  • hum...im not at my computer, could you run my code step by step and look at the value of the variables and maybe see where the mistake actaully is? – Alex Apr 02 '17 at 21:20
  • Splitstring is being used before its been assigned a value is one an error that it gives but even if I run it still doesn't get the info between the two keywords. Also says for the second if statement that parts hides a variable in an enclosed block. So two errors on the code. –  Apr 02 '17 at 21:38
0

you can use the following code with RegEx to return the whole sentence including the two keywords you are providing

Dim str As String = "first words to search for: Server 64-bit second words/characters to search for: </div>"

str = str.Replace(vbNewLine,"|")
Dim strA As String = Regex.Match(str, "Server 64-bit(.*?)</div>", RegexOptions.Singleline).Value

Msgbox(strA)

Or you can use the following expression to get only value between this two keywords:

Dim strA As String = Regex.Match(str, "(?<=Server 64-bit)(.*)(?=</div>)", RegexOptions.Singleline).Value
Hadi
  • 36,233
  • 13
  • 65
  • 124
  • I only need the very first instance of the content between Server 64-Bit and there are others in the webpage but the only one I need is the very first one that it finds. Using your code its just not working I don't know why. –  Apr 02 '17 at 21:11
  • Neither one of the solutions here are working for me. I've edited my question and added the code I'm trying to grab from the html maybe that may help. –  Apr 02 '17 at 21:14
  • @Palumbo Use `regex.match` instead of `matches` it will return the first occurence. I updated my answer take a look – Hadi Apr 02 '17 at 21:18
  • I am trying to use Dim webPage As String = WRequest("website.com", "GET", "") and then putting webPage int the str spot and it tells me that match can't be converted to string. –  Apr 02 '17 at 21:33
  • Use `match.value` chech my answer – Hadi Apr 02 '17 at 21:38
  • When run the messagebox turns out to be blank. Nothing shown. –  Apr 02 '17 at 21:41
  • No when using match it didn't return anything no messagebox nothing. –  Apr 02 '17 at 21:47
  • the error is because the string contains `vbNewLine` character, you can do a workaround and replace it with another separator. check my answer – Hadi Apr 02 '17 at 22:06
  • it worked , i used `Dim strA As String = Regex.Match(str, "(?<=Server 64-bit)(.*)(?=)", RegexOptions.Singleline).Value` – Hadi Apr 02 '17 at 22:11
  • Ty that worked. I was thinking it had to do something with the whitespace. I just figured vb.net would read past the whitespace and just skip it. –  Apr 02 '17 at 22:17
  • @Palumbo, though you got working solution, Regex isn't good approach in long time term. For example read this: [http://stackoverflow.com/a/1732454/1565525](http://stackoverflow.com/a/1732454/1565525). – Fabio Apr 03 '17 at 04:46
  • Yeah I realize its not the best but this program is for me and me only. The website I'm pursing is a very very well maintained and popular website. They will always have their html perfect so this works just fine for what I need and very simple amount of code. If I was doing a lot of html parsing I'd be using something different. Thank you though. –  Apr 04 '17 at 15:44