user41013
user41013

Reputation: 1241

VBScript Regex expression help in Excel VBA

I need to do some global changes to a number of spreadsheets and I think the best way is to use regular expressions so I have added a reference to Microsoft VBScript Regular Expression type library and can now process regular expressions. I am using a function with the following signature:

RegExpSubstitute ( target as string, searchREGEX as string, replaceREGEX as string) as string

I have a number of cells with contents like this:

'ALBERTS1:XLRI_CHEM_CHEMICAL_1' * 'Albert_Chemicals_SG_Lime' * 'Albert_Chemicals_Conc_Lime' * 'Albert_Lime_price' / 1000

All the stuff starting "'Albert_" need to be changed to "'ALBERTS1-" and all the other underscores in that symbol need to be changed to dashes. The first part I can do with:

new = RegExpSubstitute(old, "(')([^-:_ ]+)(_)([^']*)(')", "$1" & scadaPrefix & "-$4$5")

but that only replaces the first '_' with '-' and I need to do all the others in that symbol. The first symbol, starting with "'ALBERTS1:" should not be changed.

At the moment, I am thinking that the best thing to do is split the string into symbols on the space character and then process each symbol separately, but can I do the whole thing in one search & replace?

Upvotes: 0

Views: 768

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

Sorry, no, you could only do that with a regex engine that supports infinite-length-lookbehind, and ECMAScript engines don't do that.

You'll have to process each entry separately, and if it starts with Albert_, then do the replacements on it.

Upvotes: 1

Related Questions