【问题标题】:Finding a string between 2 other strings that has a minimum length of 5 a maximum of 7 and contains 2 dots在 2 个其他字符串之间找到一个字符串,该字符串的最小长度为 5,最大长度为 7,并且包含 2 个点
【发布时间】:2011-12-01 05:05:29
【问题描述】:

好的,这听起来可能有点复杂,但相信我不应该这样。

我需要做的是找到一个可以是任何值的字符串,它位于">""</" 之间,最小长度为 5,最大长度为 7,并且正好包含 2 个点。

如果我有这样的文本文件:

<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 />

我希望它只找到 5.0.77。不,数字并不总是在h2 标签之间,而且数字甚至不总是相同的。唯一不变的是它介于 "&gt;""&lt;/" 之间,并且它介于 5 到 7 个字符之间,并且包含 2 个点。

如果有人能提供帮助,我将不胜感激。

【问题讨论】:

标签: vb.net string


【解决方案1】:

类似的东西。

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

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 2020-08-19
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2017-05-13
    相关资源
    最近更新 更多