regex pattern not working in VBA

johnmerlino

Board Regular
Joined
Sep 21, 2010
Messages
94
Hey all,

in this snippet of code:

Code:
Set has_parens_with_content = CreateObject("VBScript.RegExp")

    With has_parens_with_content
        .MultiLine = False
        .Global = True
        .IgnoreCase = True
        .Pattern = "\([A-Za-z0-9]\)"

 If has_parens_with_content.test(clean_string) Then
        clean_string = has_parens_with_content.Replace(clean_string, " ")
    End If

if at any time the current cell as alphanumeric characters in parenthesis, I want to remove3 those alphanumeric characters and the parenthesis. For example, this:

abc & abcd (abc)

to this:

abc & abcd

However, when I run the above code, it does not remove the parens with alpha characters. Im pretty sure the regex is right.

thanks for response
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Not sure but try:
.Pattern = "\([A-Za-z0-9]+\)"

The character class only matches a single character - you want to match one or more such characters.
 
Upvote 0
^^ The OP reports a problem with the regex matching and not a syntax error compiling the code - so let us assume for now that End With is present in the code, though not shown. If not, the OP will tell us shortly. That's my take.

ξ
 
Upvote 0
If there is at maximum one item in parentheses (there could be none), and if there is never an unmatched parenthesis, then you do not need RegExp to do your removal...
Code:
  Dim Parts() As String
  .....
  .....
  clean_string = "(ABC) dsfsdak sdf"
  Parts = Split(Replace(clean_string & "(", ")", "("), "(")
  Parts(1) = ""
  clean_string = WorksheetFunction.Trim(Replace(Join(Parts, ""), "((", ""))
 
Upvote 0
If you are removing it, should replace them with "", not " "
Code:
Function RemoveText(r As String) As String
With CreateObject("vbscript.regexp")
    .Pattern = "\([A-Z0-9]+\)"
    .IgnoreCase = True
    .Global = True
    RemoveText = .Replace(r, "")
End With
End Function


Sub test()
Dim s As String
s = "abc & abcd (abc) & abc (abdcedef)"
MsgBox RemoveText(s)
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top