我希望我的代码在字符串中找到字母旁边有一个数字的任何地方,并在两者之间插入一个空格。
我有几个没有空格的地址,我需要通过将数字与字母分开来插入空格。例如:
123MainSt.Box123
应该是 123 Main St. Box 123
或者
123百汇134
应该是:123 Parkway 134
这是我从代码开始的地方,但它一开始就结合了两个数字......
Dim Digits() As String = Regex.Split(Address, "[^0-9]+")
'MsgBox(Digits.Count)
If Digits.Length > 2 Then
' For Each item As String In Digits
Dim Letters As String = Regex.Replace(Address, "(?:[0-9]+\.?[0-9]*|\.[0-9]+)", "")
rCell.Value = Digits(0) & Letters & Digits(1)
End If
If Digits.Length < 3 Then
If Address.Contains("th") Then
Else
Dim Part1 As String = Regex.Replace(Address, "[^0-9]+", "")
Dim Part2 As String = Regex.Replace(Address, "(?:[0-9]+\.?[0-9]*|\.[0-9]+)", "")
'MsgBox(Part1 & " " & Part2)
rCell.Value = Part1 & " " & Part2
End If
End If
最佳答案
I would like my code to find anywhere in the string that there is a number next to a letter and insert a space between the two.
您可以使用的正则表达式是
Regex.Replace(input, "(?<=\d)(?=\p{L})|(?<=\p{L})(?=\d)", " ")
第一种选择 - (?<=\d)(?=\p{L})
- 匹配数字和字母之间的位置,以及第二种选择 - (?<=\p{L})(?=\d)
- 匹配字母和数字之间的位置。请注意
(?<=\p{L})
是正向回溯,需要在当前位置和 (?=\d)
之前有一个字母是一个正向前瞻,需要在当前位置之后有一个数字。这些是不使用文本的环视,因此,您可以用 (= insert) 空格替换空格。
关于regex - 需要通过 vb.net 从字符串中的数字中分离出字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38154291/