Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named groups for Regex in VBA

Tags:

regex

excel

vba

Is there any way to use named groups with regular expressions in VBA?

I would like to write a an Excel VBA Sub that matches the dates in file names and decrements these dates by a specified amount. I need to be able to distinguish between dd/mm and mm/dd formats -- among other irregularities -- and using named groups something like this would solve the problem:

(?:<month>\d\d)(?:<day>\d\d)

Advice is appreciated

like image 234
mmmbeer Avatar asked Sep 03 '25 14:09

mmmbeer


1 Answers

Nope, no named groups in VBScript regular expressions.

VBScript uses the same regexp engine that JScript uses, so it's compatible with JavaScript regex, which also doesn't have named groups.

You have to use unnamed groups and just go by the order they appear on the expression to retrieve them by index after running it.

In general, dd/mm and mm/dd can't be automatically distinguished since there are valid dates that could be either. (e.g. 01/04 could be January 4th or April 1st). I don't think you'd be able to solve this with a regular expression.

like image 144
Tmdean Avatar answered Sep 05 '25 09:09

Tmdean