regex - 需要通过 vb.net 从字符串中的数字中分离出字母

标签 regex vb.net excel visual-studio

我希望我的代码在字符串中找到字母旁边有一个数字的任何地方,并在两者之间插入一个空格。

我有几个没有空格的地址,我需要通过将数字与字母分开来插入空格。例如:

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/

相关文章:

python - 基于正则表达式过滤数据框

regex - 泛数字正则表达式?

java - Java 中的轻量级 XLSX 阅读器

VBA:需要关闭窗口,只能关闭工作簿

javascript - Google Sheets - 循环浏览表格合并数据

javascript - 如何使用 JavaScript 按日期分割一个巨大的字符串?

javascript - 替换句子中的字符串

vb.net - 多线程运行 ffmpeg

c++ - 为什么我在 DLL 中收到 "Unable to find an entry point named ' SquareRoot' 消息?

c - 声明特定长度的字符串