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"

Thanks

Re: reggular expressions by Evertjan

Evertjan
Sun Feb 19 05:09:19 CST 2006

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


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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

Re: reggular expressions by Evertjan

Evertjan
Sun Feb 19 10:29:19 CST 2006

Steve Fulton wrote on 19 feb 2006 in microsoft.public.scripting.vbscript:

> 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")

Well done!

> End With
>
> For each match, either $1 and $2 or $3 and $4 will be empty strings.
>

No need for the (?:'s

With New RegExp
.IgnoreCase = True
.Global = True
.Pattern = "(\d)-([a-z])|([a-z])-(\d)"
rep = .Replace(x, "$1$3===$2$4")
End With


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Re: reggular expressions by asc4john

asc4john
Sun Feb 19 11:31:40 CST 2006

duh.
I forgot about using $1,$2.

But I built a great kludge.

Thanks



"asc4john" <john_kinnear@telus.net> wrote in message
news:L7QJf.3778$jh5.1902@edtnps84...
>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"
>
> Thanks
>
>
>
>