不希望的多个匹配在asp.net中创建与正则表达式匹配的链接

时间:2016-06-15 23:10:38

标签: asp.net regex

我正在使用与我的正则表达式匹配的某些字符创建一个链接,并且只要特定匹配在输入字符串中不会出现多次,所有符号都会按预期工作。

我想找到“RTR-”或“RO-”的任何实例,后跟2到4个数字,并将其转换为链接。恩。 “这是RTR-1234”变成

"This is <a href='http://server/browse/RTR-1234'>RTR-1234</a>"

我将我的字符串传递给:

Function linkifyText(ByVal txt As String) As String

    Dim regx As New Regex("\b(RTR-|RO-)\d{2,4}\b", RegexOptions.IgnoreCase)

    Dim mactches As MatchCollection = regx.Matches(txt)

    For Each match As Match In mactches
        txt = txt.Replace(match.Value, "<a href='http://server/browse/" & match.Value & "'>" & match.Value & "</a>")
    Next

    Return txt
End Function

即使存在多个不同的匹配项,这似乎也能正常工作。例如“这是RTR-1234,这是RTR-4321”变成

This is <a href='http://server/browse/RTR-1234'>RTR-1234</a> and this is <a href='http://server/browse/RTR-4321'>RTR-4321</a>

但是,当输入字符串中多次出现相同匹配时,我遇到了问题。例如,“这是RTR-1234,这又是RTR-1234”变为

This is <a href='http://server/browse/<a href='http://server/browse/RTR-1234'>RTR-1234</a>'><a href='http://server/browse/RTR-1234'>RTR-1234</a></a> again this is <a href='http://server/browse/<a href='http://server/browse/RTR-1234'>RTR-1234</a>'><a href='http://server/browse/RTR-1234'>RTR-1234</a></a>

1 个答案:

答案 0 :(得分:0)

描述

为什么不在一次搜索和替换操作中执行此操作?

\b((?:R(?:TR|O))-[0-9]{2,4})\b

替换为: <a href='http://server/browse/$1'>$1</a> Regular expression visualization

此正则表达式将执行以下操作:

  • 找到RTR-RO-后跟2到4个数字的任何实例

实施例

现场演示

https://regex101.com/r/qD4kC4/1

示例文字

I want to find any instance of "RTR-" or "RO-" followed by 2 to 4 numbers, and convert that to a link. 

ex. "This is RTR-1234" 

For example, "This is RTR-1234 again this is RTR-1234" becomes

替换后

I want to find any instance of "RTR-" or "RO-" followed by 2 to 4 numbers, and convert that to a link. 

ex. "This is <a href='http://server/browse/RTR-1234'>RTR-1234</a>" 

For example, "This is <a href='http://server/browse/RTR-1234'>RTR-1234</a> again this is <a href='http://server/browse/RTR-1234'>RTR-1234</a>" becomes

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      R                        'R'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        TR                       'TR'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        O                        'O'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
    -                        '-'
----------------------------------------------------------------------
    [0-9]{2,4}               any character of: '0' to '9' (between 2
                             and 4 times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
----------------------------------------------------------------------