Regex VB.Net Regex.Replace

前端 未结 2 648
时光说笑
时光说笑 2021-01-27 12:21

I\'m trying to perform a simple regex find and replace, adding a tab into the string after some digits as outlined below.

From

a/users/12345/badges
         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-27 12:43

    When I read, "adding a tab into the string after some digits" I think there could be more than one set of digits that can appear between forward slashes. This pattern:

    "/(\d+)/"
    

    Will capture only digits that are between forward slashes and will allow you to insert a tab like so:

    Imports System.Text.RegularExpressions
    
    Module Module1
        Sub Main()
            Dim str As String = "a/54321/us123ers/12345/badges"
            str = Regex.Replace(str, "/(\d+)/", String.Format("/$1{0}/", vbTab))
    
            Console.WriteLine(str)
            Console.ReadLine()
        End Sub
    End Module
    

    Results (NOTE: The tab spaces can vary in length):

    a/54321 /us123ers/12345 /badges
    

    When String is "a/54321/users/12345/badges" results are:

    a/54321 /users/12345    /badges
    

提交回复
热议问题