1

次の形式「ab-cd:e」の文字列を一致させる必要があります...ただし、「.b」は「:e」と同様にオプションです。a、b、c、d、および e はすべて 10 進数でなければなりません。

一致する必要がある例を次に示します: 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]*

私は何が欠けていますか?\d の [0-9] を切り替えることができることは知っていますが、読みやすくするためにこのようにしました。

私が使用している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 に答える