UDF regular expression question

mcranmoss

Board Regular
Joined
Dec 13, 2011
Messages
165
The left column in the data below is a list of filenames. I have created a UDF which extract strings which are 'product codes'.

Extracting product codes involves extracting everything except:

RP1
RP2
RP3
RP4
_ underscores
ddmmyy or ddmmyyyy or dd.mm.yy (dates)
.txt


I could only make this work in my regular expression by changing all of the above to spaces, and trimming the result.

Basically, this seemed a bit clumsy, and, for the sake of feeling more confident using regular expressions, I would like to construct it to suppress the above strings.



UY3_RP2_26.07.11.txt - UY3
UY3_RP3_26.07.11.txt - UY3
V5_RP3_090811.txt - V5
VN_RP3_21.07.11.txt - VN
VV2_RP3_09.08.11.txt - VV2
VW2_RP3_03.08.11.txt - VW2
02_RP2_B7V_270211.txt - 02 B7V
02_RP2_B7X_270211.txt - 02 B7X
02_RP3_B4X_270211.txt - 02 B4X
02_RP3_B7T_270211.txt - 02 B7T
02_RP3_B7V_270211.txt - 02 B7V



=TRIM(ExtractProductCode(A1))

Code:
Function ExtractProductCode(ByVal s As String) As String
Dim rx As Object
 
Set rx = CreateObject("VBScript.RegExp")
With rx
    .Pattern = "(RP1|RP2|RP3|RP4|\d{6,8}|_|.txt|\d{2}\.\d{2}\.\d{2})"
    .Global = True
    ExtractProductCode = .Replace(s, " ")
End With
End Function
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi

I tried this one on your examples and it worked.

Try:

Code:
Function ExtractProductCode(ByVal s As String) As String
Dim rx As Object
 
Set rx = CreateObject("VBScript.RegExp")
With rx
    .Pattern = "^(.*?)_?(?:RP[1234])_?(.*?)_?(\d\d\.\d\d\.\d\d|\d{6,8})\.txt$"
    .Global = True
    ExtractProductCode = Trim(.Replace(s, "$1 $2"))
End With
End Function
 
Upvote 0
I'm glad it helped. Thanks for the feedback.

Remark:

The statement:

Code:
    .Global = True
is not necessary. I had copied it from your code and forgot to delete it.
 
Upvote 0

Forum statistics

Threads
1,223,671
Messages
6,173,736
Members
452,532
Latest member
cnetctg

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