Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式进行大小写转换_Regex_Vba_Excel - Fatal编程技术网

Regex 使用正则表达式进行大小写转换

Regex 使用正则表达式进行大小写转换,regex,vba,excel,Regex,Vba,Excel,在VBA中使用Excel和正则表达式,我对此是新手 我需要通过strReplace命令将一个捕获组的内容从全大写转换为titlecase。我不能在strReplace的结果上使用Excel适当的函数,因为它不能提供所需的结果 示例单元格内容如下(并非所有单元格都像此单元格,这就是为什么字符串模式定义更全面): 我的字符串模式是: strPattern = "^(\D{2,4}\d{0,4})-(\d{1,4}) (.*)\s(\d\d)( HP)\s\((\d\d.\d)\skw\)" str

在VBA中使用Excel和正则表达式,我对此是新手

我需要通过strReplace命令将一个捕获组的内容从全大写转换为titlecase。我不能在strReplace的结果上使用Excel适当的函数,因为它不能提供所需的结果

示例单元格内容如下(并非所有单元格都像此单元格,这就是为什么字符串模式定义更全面):

我的字符串模式是:

strPattern = "^(\D{2,4}\d{0,4})-(\d{1,4}) (.*)\s(\d\d)( HP)\s\((\d\d.\d)\skw\)"
strReplace = "$1-$2 - Juniper $1 Part, $3, $4HP, $6KW"
我的替代模式是:

strPattern = "^(\D{2,4}\d{0,4})-(\d{1,4}) (.*)\s(\d\d)( HP)\s\((\d\d.\d)\skw\)"
strReplace = "$1-$2 - Juniper $1 Part, $3, $4HP, $6KW"
放置在相邻单元格中的当前结果为:

FDI850-4224 - Juniper FDI850 Part, JIM SMITH, 29HP, 21.6KW
FDI850-4224 - Juniper FDI850 Part, Jim Smith, 29HP, 21.6KW
我希望放置在相邻单元格中的结果是:

FDI850-4224 - Juniper FDI850 Part, JIM SMITH, 29HP, 21.6KW
FDI850-4224 - Juniper FDI850 Part, Jim Smith, 29HP, 21.6KW
是否可以在捕获组3(.*)的strReplace中使用某些内容将其从全大写改为titlecase

以下是我的VBA代码:

Function tom_test(Myrange As Range) As String

Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String

strPattern = "^(\D{2,4}\d{0,4})-(\d{1,4}) (.*)\s(\d\d)( HP) \((\d\d.\d\d)\skW\)"

If strPattern <> "" Then
    strInput = Myrange.Value
    strReplace = "$1-$2 - Juniper $1 Part, $3, $4HP, $6KW"
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = strPattern
    End With

    If regEx.Test(strInput) Then
        tom_test = regEx.Replace(strInput, strReplace)

    Else
        tom_test = "No Bueno"
    End If
End If    
End Function
如果有任何帮助,我将不胜感激。

试试这个:

Option Explicit
Function tom_test(Myrange As Range) As String

Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String

Dim MC As MatchCollection

strPattern = "^(\D{2,4}\d{0,4})-(\d{1,4}) (.*)\s(\d\d)( HP)\s\((\d\d.\d)\skw\)"

If strPattern <> "" Then
    strInput = Myrange.Value
    strReplace = "$1-$2 - Juniper $1 Part, $3, $4HP, $6KW"
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = strPattern
    End With

    If regEx.Test(strInput) Then
        Set MC = regEx.Execute(strInput)

        strReplace = Replace(strReplace, "$3", StrConv(MC(0).SubMatches(2), vbProperCase))

        tom_test = regEx.Replace(strInput, strReplace)

    Else
        tom_test = "No Bueno"
    End If
End If
End Function
选项显式
函数tom_测试(Myrange作为Range)作为字符串
Dim regEx作为新的RegExp
作为字符串的Dim strPattern
像弦一样的模糊的条纹
变暗字符串替换为字符串
Dim MC作为MatchCollection
strPattern=“^(\D{2,4}\D{0,4})-(\D{1,4})(.*)s(\D\D)(HP)\s\(\D\D\D)\skw\)
如果strPattern“”则
strInput=Myrange.Value
strReplace=“$1-$2-Juniper$1部件,$3,$4HP,$6KW”
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
Set MC=regEx.Execute(strInput)
strReplace=Replace(strReplace,“$3”,StrConv(MC(0).子匹配(2),vbProperCase))
tom_测试=正则表达式替换(strInput,strReplace)
其他的
tom_test=“不必了”
如果结束
如果结束
端函数

只需在捕获组3时使用正确的方法即可。如何做到这一点取决于您的VBA代码。谢谢。我在问题中添加了VBA代码。我不知道如何使用适当的捕捉组3。我已经发布了一些代码谢谢!我运行了它,它工作了。我有很多东西要学。谢谢你的帮助。