Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine three functions into one function to remove all possible spaces around a specific character and a digit(s)

Note: Before I post my question,
I got a suggestion that this Link to answer my question, I have checked it and it dose not.

Because there are more than one user input values on our database system (later exported into an Excel spreadsheet).
So the manual entry of multiplication pattern by users occurs to be different each time.
example 1: "3 x2"
example 2: "4x 8"
example 3: "6 x 10"
as you will notice there may be a space between "X" character and the digits in a different position each time.
Because my expertise with regular expression is on the beginning stages,
I have been created three functions to deal with each example above and they works respectively correctly as it should.
But, I seek to combine these function into one function (if it is possible) for easier use.
In Advance, great thanks for your help.

Current Values Expected Result
Last the 3 x2 Injection Last the 3×2 Injection
Last the 4x 8 Injection Last the 4×8 Injection
Last the 6 x 10 Injection Last the 6×10 Injection
Function Remove_Space_between_Digit_X(ByVal str As String) As String
   Static reg As New RegExp
     reg.Global = True
      reg.IgnoreCase = True
      
       reg.Pattern = "(\s[0-9]+)(\s)(x)"
       
    Remove_Space_between_Digit_X = re.Replace(str, "$1$3")
End Function

Function Remove_Space_between_X_Digit(ByVal str As String) As String
   Static reg As New RegExp
     reg.Global = True
      reg.IgnoreCase = True
      
       reg.Pattern = "(x)(\s)([0-9]+)(\s)"
       
    Remove_Space_between_X_Digit = reg.Replace(str, "$1$3$4")
End Function

Function Remove_Space_around_X_Digit(ByVal str As String) As String
   Static reg As New RegExp
     reg.Global = True
      reg.IgnoreCase = True
      
       reg.Pattern = "([0-9])+(\s)(x)(\s)([0-9]+)(\s)"
       
    Remove_Space_around_X_Digit = reg.Replace(str, "$1$3$5$6")
End Function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use this regex:

(\d)\s*x\s*(\d)

which will match:

  • (\d) : a digit (captured in group 1)
  • \s*x\s* : an x surrounded by some number of spaces
  • (\d) : a digit (captured in group 2)

and replace it with $1x$2 to remove any spaces in the string.

Demo on regex101

In VBA:

Function Remove_Space_around_X(ByVal str As String) As String
    Static reg As New RegExp
    reg.Global = True
    reg.IgnoreCase = True 
    reg.Pattern = "(\d)\s*x\s*(\d)"
       
    Remove_Space_around_X = reg.Replace(str, "$1x$2")
End Function
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading