开发者

Finding a string between 2 other strings that has a minimum length of 5 a maximum of 7 and contains 2 dots

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.

S开发者_如何学Pythono 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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜