1

Ok, this might sound a bit complicated, but trust me it shouldn't be.

What I need to do is find a string that can be any value, and is placed between ">" and "</" with a minimun length of 5 and a maximum length of 7 and contains exactly 2 dots.

So if I have a text file like this:

<a href="www.site.com">a site</a>
text<br />
More test<br />
<img src="maybe an images">
<h2>5.0.77</h2></br>
More text<br />

I want it to find only the 5.0.77. And no, the number isn't always between h2 tags, and the number isn't even always the same. The only thing that is static about it is that is is between ">" and "</" and that it is between 5 and 7 characters and contains 2 dots.

So if anyone could help, I'd be very grateful.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Qub1
  • 1,154
  • 2
  • 14
  • 31
  • Should this have a homework tag? – Matt Wilko Oct 05 '11 at 08:20
  • 1
    Don't think teachers are smart enough to ask questions like that ;-) – chrissie1 Oct 05 '11 at 09:27
  • Why the _random_ tag -- are you trying to generate strings like this, or find them? One other thing -- [if you are using RE to parse HTML, you are barking up the wrong tree](http://stackoverflow.com/questions/2400623/if-youre-not-supposed-to-use-regular-expressions-to-parse-html-then-how-are-htm). – Jay Elston Oct 06 '11 at 03:50

1 Answers1

2

Something like this.

Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Console.WriteLine(FindString("<h2>5.0.77</h2>"))
        Console.WriteLine(FindString("<h2>5.77</h2>"))
        Console.WriteLine(FindString("<h1>title</h1>"))
        Console.WriteLine(FindString("<h2>1575.0.77</h2>"))
        Console.WriteLine(FindString("<h2>2.0.77</h2>"))
        Console.WriteLine(FindString("5.0.77</h2>"))

        Console.ReadLine()
    End Sub

    Private Function FindString(ByVal Text As String) As String
        Dim result As String = ""
        Dim match As Match = Regex.Match(Text, ">([0-9]*\.[0-9]*\.[0-9]*)<")

        If match.Groups.Count = 2 Then
            If match.Groups(1).Value.Length >= 5 AndAlso match.Groups(1).Value.Length <= 7 Then
                result = match.Groups(1).Value
            End If
        End If

        Return result
    End Function
End Module
chrissie1
  • 5,014
  • 3
  • 26
  • 26