Regular expression to allow certain characters

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
921
Office Version
  1. 365
Platform
  1. Windows
Using Microsoft VBScript Regular Expressions 5.5.

I'm pretty close with this one, but shortly, I want to allow numbers, letters (including ÅÄÖ) and underscores. I don't want to allow special characters, like ^ or $. So the not allowed characters should simply be removed. I have a problem with that $ now, I understood that's the "end of string to test", so I think I'm supposed to include it somehow there.

This is what I have:
VBA Code:
Sub testparsename()
    Dim result As String
    result = ParseName("^Test$") 'This returns now Test$
End Sub

Public Function ParseName(original As String) As String

    Dim searchpattern As String:
    searchpattern = "[^0-9A-ZÅÄÖ_$]"
  
    Dim replacewith As String
    replacewith = vbNullString
  
    Dim result As String
    Dim regEx As New RegExp
  
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = searchpattern
  
        result = .Replace(original, replacewith)
    End With
  
    ParseName = result
End Function
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Do you simply mean you are looking to test against "^[\wÅÄÖ]+$". Since you ignore case sensitivity this would mean:
  • ^ - Match start-line anchor;
  • [\wÅÄÖ]+ - Match 1+ characters in the character class where "\w" is short for "A-Za-z0-9_", or what is called a word-character;
  • $ - End-line anchor;
 
Upvote 0
Do you simply mean you are looking to test against "^[\wÅÄÖ]+$". Since you ignore case sensitivity this would mean:
  • ^ - Match start-line anchor;
  • [\wÅÄÖ]+ - Match 1+ characters in the character class where "\w" is short for "A-Za-z0-9_", or what is called a word-character;
  • $ - End-line anchor;
That \w sounds like a really good option. But this below returns now also the caret

VBA Code:
Sub testparsename()
    Dim result As String
    result = ParseName("^Test$") 'This returns now ^Test$
End Sub

Public Function ParseName(original As String) As String

    Dim searchpattern As String:
    searchpattern = "^[\wÅÄÖ]+$"
  
    Dim replacewith As String
    replacewith = vbNullString
  
    Dim result As String
    Dim regEx As New RegExp
  
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = searchpattern
  
        result = .Replace(original, replacewith)
    End With
  
    ParseName = result
End Function
 
Upvote 0
I know you are trying to do this using Regular Expressions, but it can also be done reasonably easily without using them...
VBA Code:
Function ParseName(ByVal Original As String) As String
  Dim X As Long
  For X = 1 To Len(Original)
    If Mid(Original, X, 1) Like "[!A-Za-z0-9ÅÄÖ_]" Then
      Mid(Original, X) = "`"
    End If
  Next
  ParseName = Replace(Original, "`", "")
End Function
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,344
Members
452,638
Latest member
Oluwabukunmi

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