Re: reggular expressions by Steve
Steve
Sun Feb 19 06:21:52 CST 2006
Evertjan. wrote:
> asc4john wrote on 19 feb 2006 in microsoft.public.scripting.vbscript:
>
>> I would like to replace a "-" in a string but only when it is
>> surounded by either a letter or a number. for example
>> "A-4" or "3-V" but not "S-D" or "4-4"
>>
>
> Replaces - by ===
>
> function rep(x)
> Set regEx = New RegExp
> regEx.IgnoreCase = True
> regEx.Global = True
> regEx.Pattern = "(\d)-([a-z])"
> x = regEx.Replace(x, "$1===$2")
> regEx.Pattern = "([a-z])-(\d)"
> rep = regEx.Replace(x, "$1===$2")
> end function
Modification to replace either case in one pass:
With New RegExp
.IgnoreCase = True
.Global = True
.Pattern = "(?:(\d)-([a-z]))|(?:([a-z])-(\d))"
rep = .Replace(x, "$1$3===$2$4")
End With
For each match, either $1 and $2 or $3 and $4 will be empty strings.
--
Steve
It is far, far safer to be wrong with the majority than to be right
alone. -John Kenneth Galbraith