0

何をしようとしても、記号と一致することはありません。文字列をパターンとして使ってみました。この関数は、のような文字列を取得してオブジェクト1 day -36mins + 2s -1sに解析できる必要があります。TimeSpanポインタはありますか?

Public Function ParseTimeDelta(ByVal TimeDelta As String) As TimeSpan
    Dim DayPattern As String = "\b([+-]?\w*\d+)\s*d(?:ay[s]?)?\b"
    Dim HourPattern As String = "\b([+-]?\w*\d+)\s*h(?:(?:ou)?r[s]?)?\b"
    Dim MinutePattern As String = "\b([+-]?\w*\d+)\s*m(?:in(?:ute)?[s]?)?\b"
    Dim SecondPattern As String = "\b([+-]?\w*\d+)\s*s(?:ec(?:ond)?[s]?)?\b"
    Dim Days As Integer = 0
    Dim Hours As Integer = 0
    Dim Minutes As Integer = 0
    Dim Seconds As Integer = 0
    Dim Regex As Text.RegularExpressions.Regex

    Regex = New Text.RegularExpressions.Regex(DayPattern, Text.RegularExpressions.RegexOptions.IgnoreCase)
    For Each Match As Text.RegularExpressions.Match In Regex.Matches(TimeDelta)
        Days += CInt(Match.Groups(1).Value)
    Next

    Regex = New Text.RegularExpressions.Regex(HourPattern, Text.RegularExpressions.RegexOptions.IgnoreCase)
    For Each Match As Text.RegularExpressions.Match In Regex.Matches(TimeDelta)
        Hours += CInt(Match.Groups(1).Value)
    Next

    Regex = New Text.RegularExpressions.Regex(MinutePattern, Text.RegularExpressions.RegexOptions.IgnoreCase)
    For Each Match As Text.RegularExpressions.Match In Regex.Matches(TimeDelta)
        Minutes += CInt(Match.Groups(1).Value)
    Next

    Regex = New Text.RegularExpressions.Regex(SecondPattern, Text.RegularExpressions.RegexOptions.IgnoreCase)
    For Each Match As Text.RegularExpressions.Match In Regex.Matches(TimeDelta)
        Seconds += CInt(Match.Groups(1).Value)
    Next

    Return New TimeSpan(Days, Hours, Minutes, Seconds)
End Function
4

1 に答える 1

2

標識の前に単語の境界がなかったため、一致していませんでした。修理済み。

Public Function ParseTimeDelta(ByVal TimeDelta As String) As TimeSpan
    Dim DayPattern As String = "((?:[+-]|\b)\w*\d+)\s*d(?:ay[s]?)?\b"
    Dim HourPattern As String = "((?:[+-]|\b)\w*\d+)\s*h(?:(?:ou)?r[s]?)?\b"
    Dim MinutePattern As String = "((?:[+-]|\b)\w*\d+)\s*m(?:in(?:ute)?[s]?)?\b"
    Dim SecondPattern As String = "((?:[+-]|\b)\w*\d+)\s*s(?:ec(?:ond)?[s]?)?\b"
    Dim Days As Integer = 0
    Dim Hours As Integer = 0
    Dim Minutes As Integer = 0
    Dim Seconds As Integer = 0
    Dim Regex As Text.RegularExpressions.Regex

    Regex = New Text.RegularExpressions.Regex(DayPattern, Text.RegularExpressions.RegexOptions.IgnoreCase)
    For Each Match As Text.RegularExpressions.Match In Regex.Matches(TimeDelta)
        Days += CInt(Match.Groups(1).Value)
    Next

    Regex = New Text.RegularExpressions.Regex(HourPattern, Text.RegularExpressions.RegexOptions.IgnoreCase)
    For Each Match As Text.RegularExpressions.Match In Regex.Matches(TimeDelta)
        Hours += CInt(Match.Groups(1).Value)
    Next

    Regex = New Text.RegularExpressions.Regex(MinutePattern, Text.RegularExpressions.RegexOptions.IgnoreCase)
    For Each Match As Text.RegularExpressions.Match In Regex.Matches(TimeDelta)
        Minutes += CInt(Match.Groups(1).Value)
    Next

    Regex = New Text.RegularExpressions.Regex(SecondPattern, Text.RegularExpressions.RegexOptions.IgnoreCase)
    For Each Match As Text.RegularExpressions.Match In Regex.Matches(TimeDelta)
        Seconds += CInt(Match.Groups(1).Value)
    Next

    Return New TimeSpan(Days, Hours, Minutes, Seconds)
End Function
于 2012-06-21T22:46:56.407 に答える