1

我需要匹配以下格式的字符串“ab-cd:e”......但“.b”和“:e”一样是可选的。a、b、c、d 和 e 都应该是十进制的。

以下是一些应该匹配的示例:2.2-1509、15.2-1627.3、16.1-69.48:1、16-1.48:2、17.1-275、46.2-878.3、58.1-301、58.1-615.1。

我拥有的适用于“:e”之外的所有场景的正则表达式如下:

[^<p]?[0-9]*[\.]?[0-9]*?[\.]?[ ]?[\-][0-9]*[\.]?[0-9]*?[\:]?[0-9]*

我错过了什么?我知道我可以将 [0-9] 换成 \d 但这样做是为了提高可读性。

我正在使用的 VB.NET 代码如下:

dim lang as String

'lang would be retrieved here and is usually an HTML paragraph... below is an example
lang = "<p class=section-text>§ 2. Information is defined pursuant to § 2.2-803, 15.2-1627.3, and 16.1-69.48:1 and shall establish appropriations.</p>"

Dim r As Regex = New Regex("[^<p]?[0-9]*[\.]?[0-9]*?[\.]?[ ]?[\-][0-9]*[\.]?[0-9]*?[\:]?[0-9]*")
Dim applyEvaluator As MatchEvaluator = New MatchEvaluator(Function(m As Match) applyCodeLink(m, shouldApplyColor))
lang = r.Replace(lang, applyEvaluator)
Return lang



Private Shared Function applyCodeLink(ByVal m As Match, shouldApplyColor As Boolean) As String
  Dim rStr As String = m.Value
  rStr = "<a href=""http://lis/code.aspx?cod=" & urlSuffix & """>" & m.Value & "</a>"
  If shouldApplyColor Then rStr = wrapLinkWithColor(rStr)
  Return rStr
End Function
4

1 回答 1

1

我不知道vb,但也许这会起作用:

(?<=[^0-9])[0-9]+(\.[0-9]+)?-[0-9]+(\.[0-9]+)?(:[0-9]+)?
于 2013-10-01T16:28:10.770 回答