【问题标题】:Why Does My Regex Ignore Plus or Minus Signs?为什么我的正则表达式忽略加号或减号?
【发布时间】:2012-06-21 22:21:37
【问题描述】:

无论我尝试什么,它都与标志不匹配。我什至尝试使用字符串作为模式。这个函数应该能够接受像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

【问题讨论】:

  • 你想匹配什么?
  • @KendallFrey,好吧,为了简单起见,我会给你几秒钟的有效匹配:1s1 sec-23 seconds1secs- 10 second
  • 您应该在问题中包含这些内容,并稍微缩小问题范围。

标签: regex vb.net .net-3.5


【解决方案1】:

它不匹配,因为符号之前没有单词边界。固定。

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

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2021-09-22
    • 2019-02-08
    • 2021-12-04
    • 2018-01-21
    • 2011-08-07
    相关资源
    最近更新 更多