使用VBA正则表达式在逗号后添加空格

时间:2015-06-08 15:04:26

标签: regex excel vba excel-vba

我正在尝试使用正则表达式来查找逗号范围内的单元格,但逗号后面没有空格。然后,我想在逗号和下一个字符之间添加一个空格。例如,单元格中包含Wayne,Bruce文本,但我想将其转换为Wayne, Bruce

我有一个正则表达式模式,可以找到带空格的字符和逗号的单元格,但是当我替换它时,它会切断一些字符。

Private Sub simpleRegexSearch()
    ' adapted from http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops
    Dim strPattern As String: strPattern = "[a-zA-Z]\,[a-zA-Z]"
    Dim strReplace As String: strReplace = ", "
    Dim regEx As New RegExp
    Dim strInput As String
    Dim Myrange As Range

    Set Myrange = ActiveSheet.Range("P1:P5")

    For Each cell In Myrange
        If strPattern <> "" Then
            strInput = cell.Value

            With regEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = False
                .Pattern = strPattern
            End With

            If regEx.TEST(strInput) Then
                Debug.Print (regEx.Replace(strInput, strReplace))
            Else
                Debug.Print ("No Regex Not matched in " & cell.address)
            End If
        End If
    Next

    Set regEx = Nothing
End Sub

如果我对“韦恩,布鲁斯”这样做,我会得到“Wayn,ruce”。我如何保留这些字母,但将它们分开?

4 个答案:

答案 0 :(得分:3)

按以下方式更改代码:

Dim strPattern As String: strPattern = "([a-zA-Z]),(?=[a-zA-Z])"
Dim strReplace As String: strReplace = "$1, "

输出为Bruce, Wayne

问题是你不能在VBScript中使用后视,所以我们需要以逗号之前的字母的捕获组形式的解决方法。

对于逗号后面的字母,我们可以使用前瞻,它有这种正则表达式的风格。

因此,我们只需使用([a-zA-Z])捕获并使用反向引用$1在替换调用中恢复它。前瞻不消耗字符,所以我们被覆盖。

(编辑) REGEX EXPLANATION

  • ([a-zA-Z]) - 包含仅匹配1个英文字符
  • 的字符类的捕获组
  • , - 匹配文字,(你实际上不必逃避它,因为它不是特殊字符)
  • (?=[a-zA-Z]) - 如果逗号后面的直接字符是和英文字母,则只检查(不匹配或消耗)的正面预测。

答案 1 :(得分:2)

如果我们用逗号+空格替换所有逗号,然后用逗号+空格<替换逗号+空格+空格 / em>,我们可以满足您的要求:

Sub NoRegex()
   Dim r As Range
   Set r = Range("P1:P5")
   r.Replace What:=",", Replacement:=", "
   r.Replace What:=",  ", Replacement:=", "
End Sub

答案 2 :(得分:1)

使用与stribizhev解决方案相同的RegExp,但速度有两个优化

  1. 您当前的代码会为每个测试的单元格设置Private Sub simpleRegexSearch() ' adapted from http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops Dim strPattern As String: Dim strReplace As String: Dim regEx As Object Dim strInput As String Dim X, X1 Dim lngnct Set regEx = CreateObject("vbscript.regexp") strPattern = "([a-zA-Z])\,(?=[a-zA-Z])" strReplace = "$1, " With regEx .Global = True .MultiLine = True .IgnoreCase = False .Pattern = strPattern X = ActiveSheet.Range("P1:P5").Value2 For X1 = 1 To UBound(X) If .TEST(X(X1, 1)) Then Debug.Print .Replace(X(X1, 1), strReplace) Else Debug.Print ("No Regex Not matched in " & [p1].Offset(X1 - 1).Address(0, 0)) End If Next End With Set regEx = Nothing End Sub 详细信息,这些只需要设置一次。
  2. 通过varinat数组循环比单元格范围快得多
  3. if (date('w', $current) != 0){
    

答案 3 :(得分:0)

您通过Regex所做的是找到一种模式

(any Alphabet),(any Alphabet)

然后将此类模式替换为

,_ 

其中_表示空格。

因此,如果您有Wayne,Bruce,则模式与e,B匹配。因此,结果变为Wayn, ruce

尝试

Dim strPattern As String: strPattern = "([a-zA-Z]),([a-zA-Z])"
Dim strReplace As String: strReplace = "$1, $2"