0

I'm trying to trim a string and remove any vowel and white space and duplicate characters.

Here's the code I'm using

Function TrimString(strString As String) As String
   Dim objRegex As Object

   Set objRegex = CreateObject("vbscript.regexp")

   With objRegex
       .Global = True
       .IgnoreCase = True
       .Pattern = "[aeiou\s]"
       TrimString = .Replace(strString, vbNullString)
       .Pattern = "(.)\1+"
       TrimString = .Replace(TrimString, "$1")
   End With
End Function

Is there a way to combine both patterns instead of doing this in 2 steps?

Thank you in advance.

Zack
  • 2,220
  • 1
  • 8
  • 12
user3286479
  • 415
  • 3
  • 15
  • 26

2 Answers2

0

This would work:

With objRegex
    .Global = True
    .IgnoreCase = True
    .Pattern = ".*?([^aeiou\s]).*?"
    TrimString = .Replace(TrimString, "$1$1")
End With

I'm not familiar with VBA but if there is a way to just return matches instead of replacing them in the original string then you could use the following pattern

[^aeiou\s]

And return this:

$&$&
WofWca
  • 571
  • 3
  • 11
0

You have two replacements:

  1. Removing [aeiou\s] matches, e.g. niarararrrrreghtatt turns into nrrrrrrrghttt
  2. Replacing each chunk of identical chars with the first occurrence turns nrrrrrrrghttt into nrght.

That means, you need to match the first pattern both as a separate alternative and as a "filler" between the identical chars.

The pattern you may use is

.pattern = "[aeiou\s]+|([^aeiou\s])(?:[aeiou\s]*\1)+"
TrimString = .Replace(strString, "$1")

See this regex demo.

Details

  • [aeiou\s]+ - 1+ vowels or whitespace chars
  • | - or
  • ([^aeiou\s]) - Capturing group 1: any char other than a vowel or a whitespace char
  • (?:[aeiou\s]*\1)+ - 1 or more occurrences of:
    • [aeiou\s]* - 0+ vowel or whitespace chars
    • \1 - backreference to Group 1, its value

Note that . is changed into [^aeiou\s] since the opposite has already been handled with the first alternation branch.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563