Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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-如何测试2个str模式,并根据匹配的str模式进行替换_Regex_Excel_Vba - Fatal编程技术网

Regex-如何测试2个str模式,并根据匹配的str模式进行替换

Regex-如何测试2个str模式,并根据匹配的str模式进行替换,regex,excel,vba,Regex,Excel,Vba,目前,我在Excel VBA中有两个独立的子组件。每个子项搜索不同的字符串模式,然后进行替换 Sub 1在目标字符串中搜索前导0,将其去掉,并将内容放在单独的单元格中 Sub2在目标字符串中搜索终端“99”,将“99”替换为Xs,并将内容放在单独的单元格中 我执行此特定操作的方法是首先运行Sub1。结果放在AO列中。然后,我对从Sub1获得的结果运行Sub2,并将这些结果放在下一个相邻的列中 我想结合这两个sub,只运行一次就可以得到想要的结果 下面是我对其应用正则表达式的W列中目标字符串的示例

目前,我在Excel VBA中有两个独立的子组件。每个子项搜索不同的字符串模式,然后进行替换

Sub 1在目标字符串中搜索前导0,将其去掉,并将内容放在单独的单元格中

Sub2在目标字符串中搜索终端“99”,将“99”替换为Xs,并将内容放在单独的单元格中

我执行此特定操作的方法是首先运行Sub1。结果放在AO列中。然后,我对从Sub1获得的结果运行Sub2,并将这些结果放在下一个相邻的列中

我想结合这两个sub,只运行一次就可以得到想要的结果

下面是我对其应用正则表达式的W列中目标字符串的示例:

098765-9876-77
333222-7777-G5
9876-078-99
9867x77A
子1

Sub-tom\u-briggs\u-test\u-leading\u-zero()
'此子项搜索目标字符串中的前导零并将其删除。
Dim regEx作为新的RegExp
作为字符串的Dim strPattern
像弦一样的模糊的条纹
变暗字符串替换为字符串
将Myrange变暗为Range
设置Myrange=ActiveSheet.Range(“w2:w73352”)
对于Myrange中的每个单元格
strPattern=“^0(.*)”
如果strPattern“”则
strInput=cell.Value
strReplace=“$1”
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
单元格偏移量(0,18)=正则表达式替换(strInput,strReplace)
其他的
单元偏移量(0,18)=strInput
如果结束
如果结束
下一个
端接头
次级2

Sub-tom\u-briggs\u-test\u-trailing\u 99()
'此子项在目标字符串中搜索Terminal 99并替换它们
'与-XX。
Dim regEx作为新的RegExp
作为字符串的Dim strPattern
像弦一样的模糊的条纹
变暗字符串替换为字符串
将Myrange变暗为Range
设置Myrange=ActiveSheet.Range(“AO2:AO73352”)
'AO是放置Sub1结果的列
对于Myrange中的每个单元格
strPattern=“(.*)-99$”
如果strPattern“”则
strInput=cell.Value
strReplace=“$1-XX”
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
单元格偏移量(0,1)=正则表达式替换(strInput,strReplace)
其他的
单元偏移量(0,1)=strInput
如果结束
如果结束
下一个
端接头

谢谢您的考虑。

您不需要正则表达式。只需从以下代码中获取一个提示:

    Sub test()
    Set myRange = Sheet1.Range("A1:A2")     'Change this range as per your requirement
    For Each cell In myRange
        strInput = cell.Value
        'Checking if the 1st number is 0 or not
        If CInt(Mid(strInput, 1, 1)) = 0 Then
            strInput = Mid(strInput, 2)
        End If
        'Checking if -99 is present in the end or not
        If StrComp("-99", Right(strInput, 3), 1) = 0 Then
            strInput = Left(strInput, Len(strInput) - 3) & "-XX"
        End If
        'If there was a leading 0 or a trailing 99, then only write the updated value in another cell   
        If StrComp(cell.Value, strInput, 1) <> 0 Then
            cell.Offset(0, 1).Value = strInput
        End If
    Next
    End Sub
子测试()
设置myRange=Sheet1.范围(“A1:A2”)'根据您的要求更改此范围
对于myRange中的每个单元格
strInput=cell.Value
'检查第一个数字是否为0
如果CInt(Mid(strInput,1,1))=0,则
strInput=中间(strInput,2)
如果结束
'检查最后是否存在-99
如果StrComp(“-99”,右(strInput,3),1)=0,则
strInput=左(strInput,Len(strInput)-3)和“-XX”
如果结束
'如果有前导0或尾随99,则只在另一个单元格中写入更新后的值
如果StrComp(cell.Value,strInput,1)为0,则
单元格偏移量(0,1)。值=strInput
如果结束
下一个
端接头
这个怎么样:

Sub tom_briggs_fix_head_and_tail()
    'This sub removes a leading zero in the target string and
    'replaces trailing 99s in the target string with -XX.

    Dim regExHead As New RegExp
    Dim strHeadPattern As String
    Dim strHeadReplace As String

    Dim regExTail As New RegExp
    Dim strTailPattern As String
    Dim strTailReplace As String

    Dim strInput As String
    Dim Myrange As Range
    Dim c As Range

    Set Myrange = ActiveSheet.Range("w2:w73352")
    strHeadPattern = "^0(.*)"
    strHeadReplace = "$1"
    strTailPattern = "(.*)-99$"
    strTailReplace = "$1-XX"

    With regExHead
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = strHeadPattern
    End With

    With regExTail
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = strTailPattern
    End With

    For Each c In Myrange
        strInput = c.Value
        strInput = IIf(regExHead.Test(strInput), _
                regExHead.Replace(strInput, strHeadReplace), strInput)
        strInput = IIf(regExTail.Test(strInput), _
                regExTail.Replace(strInput, strTailReplace), strInput)
        c.Offset(0, 19) = strInput
    Next
End Sub

希望这有助于我的道歉。我应该提到,W列中的数据被归类为文本。我已经编辑了发布的问题,以显示目标文本的几个示例。我尝试了发布的答案中的代码,但无法让它提供结果。#xidgel-您的代码正是我所需要的。我将从你的帮助中学习。
    Sub test()
    Set myRange = Sheet1.Range("A1:A2")     'Change this range as per your requirement
    For Each cell In myRange
        strInput = cell.Value
        'Checking if the 1st number is 0 or not
        If CInt(Mid(strInput, 1, 1)) = 0 Then
            strInput = Mid(strInput, 2)
        End If
        'Checking if -99 is present in the end or not
        If StrComp("-99", Right(strInput, 3), 1) = 0 Then
            strInput = Left(strInput, Len(strInput) - 3) & "-XX"
        End If
        'If there was a leading 0 or a trailing 99, then only write the updated value in another cell   
        If StrComp(cell.Value, strInput, 1) <> 0 Then
            cell.Offset(0, 1).Value = strInput
        End If
    Next
    End Sub
Sub tom_briggs_fix_head_and_tail()
    'This sub removes a leading zero in the target string and
    'replaces trailing 99s in the target string with -XX.

    Dim regExHead As New RegExp
    Dim strHeadPattern As String
    Dim strHeadReplace As String

    Dim regExTail As New RegExp
    Dim strTailPattern As String
    Dim strTailReplace As String

    Dim strInput As String
    Dim Myrange As Range
    Dim c As Range

    Set Myrange = ActiveSheet.Range("w2:w73352")
    strHeadPattern = "^0(.*)"
    strHeadReplace = "$1"
    strTailPattern = "(.*)-99$"
    strTailReplace = "$1-XX"

    With regExHead
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = strHeadPattern
    End With

    With regExTail
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = strTailPattern
    End With

    For Each c In Myrange
        strInput = c.Value
        strInput = IIf(regExHead.Test(strInput), _
                regExHead.Replace(strInput, strHeadReplace), strInput)
        strInput = IIf(regExTail.Test(strInput), _
                regExTail.Replace(strInput, strTailReplace), strInput)
        c.Offset(0, 19) = strInput
    Next
End Sub