Substitute year dates with four digits to two digits

Sir*_*ack 3 regex excel vba excel-formula

Well, what i'm trying to do is replace years with 4 digits to 2 digits like

2018 to only 18

Ex: "FIAT - 147 / Elba / Fiorino / Oggi / Panorama / Premio / Spazio / Uno - 01/1976 - 12/2001 - Mille - 1.0 8V / 1.050 / 1.3 / 1.5"

It would be like "FIAT - 147 / Elba / Fiorino / Oggi / Panorama / Premio / Spazio / Uno - 01/76 - 12/01 - Mille - 1.0 8V / 1.050 / 1.3 / 1.5"

I tried to use regex.replace but it didnt work...

Sub test()
    Dim x, z
    y = "FIAT - 147 / Elba / Fiorino / Oggi / Panorama / Premio / Spazio / Uno - 01/1976 - 12/2001 - Mille - 1.0 8V / 1.050 / 1.3 / 1.5"
    Set regx = CreateObject("vbscript.regexp")
    regx.Pattern = "(\d{4})"
    regx.Global = True
    Set x = regx.Execute(y)
    Debug.Print regx.Replace(y, Right(x.Item(0), 2))
End Sub
Run Code Online (Sandbox Code Playgroud)

It will return this

FIAT - 147 / Elba / Fiorino / Oggi / Panorama / Premio / Spazio / Uno - 01/76 - 12/76 - Mille - 1.0 8V / 1.050 / 1.3 / 1.5
Run Code Online (Sandbox Code Playgroud)

Any help? i tried to use regex just to find 4 years digits, but if you have any idea with formulas its fine

Wik*_*żew 5

Use .Replace directly with a regex that matches the first 2 digits of a year and matches and captures the last 2 digits:

y = "FIAT - 147 / Elba / Fiorino / Oggi / Panorama / Premio / Spazio / Uno - 01/1976 - 12/2001 - Mille - 1.0 8V / 1.050 / 1.3 / 1.5"
Set regx = CreateObject("vbscript.regexp")
regx.Pattern = "\b\d{2}(\d{2})\b"
regx.Global = True
Debug.Print regx.Replace(y, "$1")
Run Code Online (Sandbox Code Playgroud)

Output:

FIAT - 147 / Elba / Fiorino / Oggi / Panorama / Premio / Spazio / Uno - 01/76 - 12/01 - Mille - 1.0 8V / 1.050 / 1.3 / 1.5
Run Code Online (Sandbox Code Playgroud)

See the regex demo.

The \b\d{2}(\d{2})\b pattern matches

  • \b - a word boundary
  • \d{2} - any 2 digits
  • (\d{2}) - Group 1 ($1 refers to this value from the replacement pattern): the ending two digits of a year
  • \b - a word boundary

The word boundaries may be removed in case your year values may be glued to other digits, letters or _ chars. The regex may be further adjusted for those positions, too.