I have a text containing expressions that may be generated by the regular expression “[1-9]?[0-9].[1-9]?[0-9].[1-9]?[0-9]”.
No I want to perform a regular replacement that puts a “0” in front of each part when there is no first digit. So for example the string “1.10.3” would turn into “01.10.03”.
My problem is that I don’t know how to capture the first digit if and only if there there is no second digit in the first part. Using something like “.?([0-9])[^0-9]” would capture the first digit even if there is another one. So “12.3.4” would be turned into “02.03.04” or “102.03.04”.
Do you have any idea if there is any regular expression that matches this case?
[Edit:]
I am using the VBScript Regexp Object v5.5 which supports lookaheads.
I tried to use the Regular Expression provided by the the answer of Mark Byers:
Set re = New RegExp
re.Global = True
re.Pattern = "(?:^|[^0-9])([0-9])(?![0-9])"
Dim x As String
x = "1.3.4 string"
x = re.replace(x, "0$1")
MsgBox (x)
unfortunately the result of this code is “010304 string” – the dots are stripped away. I tried to modify the pattern to match the dots and whitespaces like “(?:^|[^0-9])([0-9])([. ])” and then to replace with “0$1$2” but the result is the same.
Do you have any idea what the problem might be?
Using lookarounds you can find digits that are not next to another digit:
And without a lookbehind (as it is not supported by some popular regular expression engines):
See the second version online: Rubular
Update
Try this: