Remove punctuation using RegEx

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I'm looking for some assistance in a UDF to remove punctuation. Since I'm keeping diacritics, I can't use the usual [^A-Za-a0-9 ]. Looking at ASCII, I came up with the UDF below, but it doesn't work (at all).

Thoughts?? Thanks y'all.

Code:
Function remove_punctII(rts As String) As String

  With CreateObject("vbscript.regexp")
    .Global = True
    .IgnoreCase = True
    .Pattern = "^[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]"
    
    remove_punctII = CStr(.Replace(rts, vbNullString))
  End With

End Function
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Can you list for us EITHER the characters you want to allow OR the characters you want to disallow?
 
Upvote 0
Rick,

I was looking to remove the following:
!“#$%&‘()*+,–./:;<=>?@[\]^_`{|}~
Hex codes from: https://access-excel.tips/excel-vba-asc-function-get-ascii-value-text/

However, I think I figured it out (lots of trial/error), by removing the ^
Code:
.Pattern = "[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]"

However, if there's a more efficient way, I'd be happy to learn.

Thanks much.
 
Last edited:
Upvote 0
Rick,

I was looking to remove the following:
!“#$%&‘()*+,–./:;<=>?@[\]^_`{|}~
Hex codes from: https://access-excel.tips/excel-vba-asc-function-get-ascii-value-text/
First off, while I don't do Regular Expressions any more (last time I touched them was the late 1990's), I am pretty sure you can just put those characters between the squared brackets directly (doubling up the quote mark because it is located in a text constant defined by quotes) instead of playing with Hex values. With that said, here is a way to do what you want without using Regular Expressions...
Code:
[table="width: 500"]
[tr]
	[td]Function RemovePunct(ByVal S As String) As String
  Dim X As Long
  For X = 1 To Len(S)
    If InStr("!""#$%&‘()*+,–./:;<=>?@[\]^_`{|}~", Mid(S, X, 1)) Then Mid(S, X) = Chr(1)
  Next
  RemovePunct = Replace(S, Chr(1), "")
End Function[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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