new to regular expression UDF

mcranmoss

Board Regular
Joined
Dec 13, 2011
Messages
165
I am new to regular expressons. This first example of a regular expression does work as intended


HTML:
Function onlynumbers(ByVal ref As String)
Dim rx As Object
Set rx = CreateObject("VBScript.RegExp")
With rx
    .Pattern = "\D"
    .Global = True
    onlynumbers = .Replace(ref, " ")
End With
End Function


But this one is supposed to reduce multiple spaces to one space, and doesn't work:

HTML:
Function test(ByVal ref As String)
Dim rx As Object
Set rx = CreateObject("VBScript.RegExp")
With rx
.Pattern = "(\S+)\x20{2,}(?=\S+)"
.Global = True
End With
End Function


I've clearly not grasped the structure of this. All advice welcome.

Mark
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi Mark

The second function:

- executes no action
- returns nothing

To reduce multiple spaces to one space, try:

Code:
Function TrimSpaces(ByVal s As String) As String
Dim rx As Object
 
Set rx = CreateObject("VBScript.RegExp")
With rx
    .Pattern = "\s+"
    .Global = True
    TrimSpaces = .Replace(s, " ")
End With
End Function
 
Upvote 0
Thats great! thanks.

I struggling to customize this, so clearly haven't fully understood the syntax etc. If someone has ample time could they tackle the following questions for me?

So in the 'retain only numerals' example the .Pattern is set to \D (everything that's not a digit) so 'ref' in .Replace(ref, "/") refers to the pattern selected, and replaces it.

What is the syntax for adding multiple options and combinations to the Pattern property? \D and \s

In order to help me grasp this could someone configure this to extract 4 digits from a given string?

HTML:
Function fourdig(strData As String) As String
...
    Set RE = CreateObject("vbscript.regexp")
    With RE
        .MultiLine = False
        .Global = False
        .IgnoreCase = True
        .Pattern = "[0-9][0-9][0-9][0-9]"...
End Function


many thanks,

Mark
 
Upvote 0
You can match the pattern and get the value from the matches collection.

Code:
Function fourdig(strData As String) As String
Dim rx As Object
Dim rxMatches As Object
 
    Set rx = CreateObject("vbscript.regexp")
    With rx
        .Global = False
        .Pattern = "\d{4}"
 
        Set rxMatches = .Execute(strData)
        fourdig = rxMatches(0)
    End With
End Function

Remark: maybe you should start by reading an introduction to regular expressions, and then study some examples. If you google regular expressions you'll find millions of matches. You can start for ex. here:

http://www.aivosto.com/vbtips/regex.html


Also in my previous code a better match would be "\s{2,}" as you only need to do the replacement if there are at least 2 contigous spaces.
 
Upvote 0
That's great.

Your suggestion about studying is very sound. I have been baffled by the online examples I have so far encountered. Your examples have been a great help!

Mark
 
Upvote 0

Forum statistics

Threads
1,223,673
Messages
6,173,744
Members
452,533
Latest member
Alex19k

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