Regex VB.Net Regex.Replace

前端 未结 2 646
时光说笑
时光说笑 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:49

    You can achieve that with a mere look-ahead that will find the position right before the last /:

    Dim s As String = Regex.Replace("a/users/12345/badges", "(?=/[^/]*$)", vbTab)
    

    Output:

    a/users/12345   /badges
    

    See IDEONE demo

    Or, you can just use LastIndexOf owith Insert:

    Dim str2 As String
    Dim str As String = "a/users/12345/badges"
    Dim idx = str.LastIndexOf("/")
    If idx > 0 Then
       str2 = str.Insert(idx, vbTab)
    End If
    

提交回复
热议问题