Excel VBA - Multiple regex pattern deletion
In Excel VBA I need to perform multiple regular expression matches which then deletes the match from the string while preserving the remainder of the string. I have it working by daisy-chaining two variables, and by not testing the pattern match first since the second match is the remainder of the first.
Consider the follow data:
(2.5.3) A. 100% of product will be delivered in 3 days
(2.5.3) B. Capability to deliver product by air.
(2.5.3) C. Support for xyz feature
(2.5.3) D. Vendor is to provide an overview of the network as proposed.
(2.5.3) E. The network should allow CustomerABC to discover their devices.
(2.5.3) F. The use of CustomerABC existing infrastructure should be optimized. CustomerABC's capability will vary.
(2.5.3) G. Describe the number of network devices requiring to run CustomerABC's center.
With this data, I am deleting the outline numbers in the beginning of the string, as well as any references to CustomerABC and any hyphenation that could possibly appear multiple times in the string at any location, with potentially upper and lower case. I have the regex's working. Here is the code I'm trying:
Function test(Txt As String) As String
Dim regEx As Object
Dim v1 As String
Dim v2 As String
Dim n As String
n = "CustomerABC"
If regEx Is Nothing Then
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
End If
If Len(Txt) > 0 Then
With regEx
' The 1st pattern
.Pattern = "^(?[0-9.]+)?"
'If Not .Test(Txt) Then Exit Function
v1 = .Replace(Txt, "")
' The 2nd pattern
.Pattern = n + "(S*)?(s+)?"
'If Not .Test(Txt) Then Exit Function
v2 = .Replace(v1, "")
' The result
test = Application.Trim(v2)
End With
End If
End Function
Is there a way to make this better, speed things up, and have a variable number of match/deletions?
Thanks in advance.
excel vba excel-vba
add a comment |
In Excel VBA I need to perform multiple regular expression matches which then deletes the match from the string while preserving the remainder of the string. I have it working by daisy-chaining two variables, and by not testing the pattern match first since the second match is the remainder of the first.
Consider the follow data:
(2.5.3) A. 100% of product will be delivered in 3 days
(2.5.3) B. Capability to deliver product by air.
(2.5.3) C. Support for xyz feature
(2.5.3) D. Vendor is to provide an overview of the network as proposed.
(2.5.3) E. The network should allow CustomerABC to discover their devices.
(2.5.3) F. The use of CustomerABC existing infrastructure should be optimized. CustomerABC's capability will vary.
(2.5.3) G. Describe the number of network devices requiring to run CustomerABC's center.
With this data, I am deleting the outline numbers in the beginning of the string, as well as any references to CustomerABC and any hyphenation that could possibly appear multiple times in the string at any location, with potentially upper and lower case. I have the regex's working. Here is the code I'm trying:
Function test(Txt As String) As String
Dim regEx As Object
Dim v1 As String
Dim v2 As String
Dim n As String
n = "CustomerABC"
If regEx Is Nothing Then
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
End If
If Len(Txt) > 0 Then
With regEx
' The 1st pattern
.Pattern = "^(?[0-9.]+)?"
'If Not .Test(Txt) Then Exit Function
v1 = .Replace(Txt, "")
' The 2nd pattern
.Pattern = n + "(S*)?(s+)?"
'If Not .Test(Txt) Then Exit Function
v2 = .Replace(v1, "")
' The result
test = Application.Trim(v2)
End With
End If
End Function
Is there a way to make this better, speed things up, and have a variable number of match/deletions?
Thanks in advance.
excel vba excel-vba
You can pass in an array of patterns along with the string to be modified, or define an array within your function and then loop over it and perform the replacements.
– Tim Williams
Nov 14 '18 at 16:10
Hi Tim, I saw your post: stackoverflow.com/questions/9672436/…, but was unsure how to adapt it to my particular needs based on the fact that I am deleting and keeping remainders. May I have an example?
– Bob
Nov 14 '18 at 16:17
add a comment |
In Excel VBA I need to perform multiple regular expression matches which then deletes the match from the string while preserving the remainder of the string. I have it working by daisy-chaining two variables, and by not testing the pattern match first since the second match is the remainder of the first.
Consider the follow data:
(2.5.3) A. 100% of product will be delivered in 3 days
(2.5.3) B. Capability to deliver product by air.
(2.5.3) C. Support for xyz feature
(2.5.3) D. Vendor is to provide an overview of the network as proposed.
(2.5.3) E. The network should allow CustomerABC to discover their devices.
(2.5.3) F. The use of CustomerABC existing infrastructure should be optimized. CustomerABC's capability will vary.
(2.5.3) G. Describe the number of network devices requiring to run CustomerABC's center.
With this data, I am deleting the outline numbers in the beginning of the string, as well as any references to CustomerABC and any hyphenation that could possibly appear multiple times in the string at any location, with potentially upper and lower case. I have the regex's working. Here is the code I'm trying:
Function test(Txt As String) As String
Dim regEx As Object
Dim v1 As String
Dim v2 As String
Dim n As String
n = "CustomerABC"
If regEx Is Nothing Then
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
End If
If Len(Txt) > 0 Then
With regEx
' The 1st pattern
.Pattern = "^(?[0-9.]+)?"
'If Not .Test(Txt) Then Exit Function
v1 = .Replace(Txt, "")
' The 2nd pattern
.Pattern = n + "(S*)?(s+)?"
'If Not .Test(Txt) Then Exit Function
v2 = .Replace(v1, "")
' The result
test = Application.Trim(v2)
End With
End If
End Function
Is there a way to make this better, speed things up, and have a variable number of match/deletions?
Thanks in advance.
excel vba excel-vba
In Excel VBA I need to perform multiple regular expression matches which then deletes the match from the string while preserving the remainder of the string. I have it working by daisy-chaining two variables, and by not testing the pattern match first since the second match is the remainder of the first.
Consider the follow data:
(2.5.3) A. 100% of product will be delivered in 3 days
(2.5.3) B. Capability to deliver product by air.
(2.5.3) C. Support for xyz feature
(2.5.3) D. Vendor is to provide an overview of the network as proposed.
(2.5.3) E. The network should allow CustomerABC to discover their devices.
(2.5.3) F. The use of CustomerABC existing infrastructure should be optimized. CustomerABC's capability will vary.
(2.5.3) G. Describe the number of network devices requiring to run CustomerABC's center.
With this data, I am deleting the outline numbers in the beginning of the string, as well as any references to CustomerABC and any hyphenation that could possibly appear multiple times in the string at any location, with potentially upper and lower case. I have the regex's working. Here is the code I'm trying:
Function test(Txt As String) As String
Dim regEx As Object
Dim v1 As String
Dim v2 As String
Dim n As String
n = "CustomerABC"
If regEx Is Nothing Then
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
End If
If Len(Txt) > 0 Then
With regEx
' The 1st pattern
.Pattern = "^(?[0-9.]+)?"
'If Not .Test(Txt) Then Exit Function
v1 = .Replace(Txt, "")
' The 2nd pattern
.Pattern = n + "(S*)?(s+)?"
'If Not .Test(Txt) Then Exit Function
v2 = .Replace(v1, "")
' The result
test = Application.Trim(v2)
End With
End If
End Function
Is there a way to make this better, speed things up, and have a variable number of match/deletions?
Thanks in advance.
excel vba excel-vba
excel vba excel-vba
asked Nov 14 '18 at 16:06
BobBob
193
193
You can pass in an array of patterns along with the string to be modified, or define an array within your function and then loop over it and perform the replacements.
– Tim Williams
Nov 14 '18 at 16:10
Hi Tim, I saw your post: stackoverflow.com/questions/9672436/…, but was unsure how to adapt it to my particular needs based on the fact that I am deleting and keeping remainders. May I have an example?
– Bob
Nov 14 '18 at 16:17
add a comment |
You can pass in an array of patterns along with the string to be modified, or define an array within your function and then loop over it and perform the replacements.
– Tim Williams
Nov 14 '18 at 16:10
Hi Tim, I saw your post: stackoverflow.com/questions/9672436/…, but was unsure how to adapt it to my particular needs based on the fact that I am deleting and keeping remainders. May I have an example?
– Bob
Nov 14 '18 at 16:17
You can pass in an array of patterns along with the string to be modified, or define an array within your function and then loop over it and perform the replacements.
– Tim Williams
Nov 14 '18 at 16:10
You can pass in an array of patterns along with the string to be modified, or define an array within your function and then loop over it and perform the replacements.
– Tim Williams
Nov 14 '18 at 16:10
Hi Tim, I saw your post: stackoverflow.com/questions/9672436/…, but was unsure how to adapt it to my particular needs based on the fact that I am deleting and keeping remainders. May I have an example?
– Bob
Nov 14 '18 at 16:17
Hi Tim, I saw your post: stackoverflow.com/questions/9672436/…, but was unsure how to adapt it to my particular needs based on the fact that I am deleting and keeping remainders. May I have an example?
– Bob
Nov 14 '18 at 16:17
add a comment |
1 Answer
1
active
oldest
votes
Like this:
Function test(Txt As String) As String
Static regEx As Object '<< need Static here
Dim rv As String, p, n
n = "CustomerABC"
If regEx Is Nothing Then
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
End If
If Len(Txt) > 0 Then
rv = Txt
'looping over an array of patterns
For Each p In Array("^(?[0-9.]+)?", n & "(S*)?(s+)?")
With regEx
.Pattern = p
rv = .Replace(rv, "")
End With
Next p
End If
test = Application.Trim(rv)
End Function
Boom! Thank you very kindly Tim.
– Bob
Nov 14 '18 at 17:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304354%2fexcel-vba-multiple-regex-pattern-deletion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Like this:
Function test(Txt As String) As String
Static regEx As Object '<< need Static here
Dim rv As String, p, n
n = "CustomerABC"
If regEx Is Nothing Then
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
End If
If Len(Txt) > 0 Then
rv = Txt
'looping over an array of patterns
For Each p In Array("^(?[0-9.]+)?", n & "(S*)?(s+)?")
With regEx
.Pattern = p
rv = .Replace(rv, "")
End With
Next p
End If
test = Application.Trim(rv)
End Function
Boom! Thank you very kindly Tim.
– Bob
Nov 14 '18 at 17:44
add a comment |
Like this:
Function test(Txt As String) As String
Static regEx As Object '<< need Static here
Dim rv As String, p, n
n = "CustomerABC"
If regEx Is Nothing Then
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
End If
If Len(Txt) > 0 Then
rv = Txt
'looping over an array of patterns
For Each p In Array("^(?[0-9.]+)?", n & "(S*)?(s+)?")
With regEx
.Pattern = p
rv = .Replace(rv, "")
End With
Next p
End If
test = Application.Trim(rv)
End Function
Boom! Thank you very kindly Tim.
– Bob
Nov 14 '18 at 17:44
add a comment |
Like this:
Function test(Txt As String) As String
Static regEx As Object '<< need Static here
Dim rv As String, p, n
n = "CustomerABC"
If regEx Is Nothing Then
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
End If
If Len(Txt) > 0 Then
rv = Txt
'looping over an array of patterns
For Each p In Array("^(?[0-9.]+)?", n & "(S*)?(s+)?")
With regEx
.Pattern = p
rv = .Replace(rv, "")
End With
Next p
End If
test = Application.Trim(rv)
End Function
Like this:
Function test(Txt As String) As String
Static regEx As Object '<< need Static here
Dim rv As String, p, n
n = "CustomerABC"
If regEx Is Nothing Then
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
End If
If Len(Txt) > 0 Then
rv = Txt
'looping over an array of patterns
For Each p In Array("^(?[0-9.]+)?", n & "(S*)?(s+)?")
With regEx
.Pattern = p
rv = .Replace(rv, "")
End With
Next p
End If
test = Application.Trim(rv)
End Function
answered Nov 14 '18 at 17:22
Tim WilliamsTim Williams
87k97086
87k97086
Boom! Thank you very kindly Tim.
– Bob
Nov 14 '18 at 17:44
add a comment |
Boom! Thank you very kindly Tim.
– Bob
Nov 14 '18 at 17:44
Boom! Thank you very kindly Tim.
– Bob
Nov 14 '18 at 17:44
Boom! Thank you very kindly Tim.
– Bob
Nov 14 '18 at 17:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304354%2fexcel-vba-multiple-regex-pattern-deletion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can pass in an array of patterns along with the string to be modified, or define an array within your function and then loop over it and perform the replacements.
– Tim Williams
Nov 14 '18 at 16:10
Hi Tim, I saw your post: stackoverflow.com/questions/9672436/…, but was unsure how to adapt it to my particular needs based on the fact that I am deleting and keeping remainders. May I have an example?
– Bob
Nov 14 '18 at 16:17