Hello,

I just need to know how to find the third backslash in a string like this:

\\192.168.1.2\SYSVOL

It is like it was really \\anything\anything

and then replace it by a comma so that the string can be like:

\\192.168.1.2,SYSVOL

I have been trying with regular expressions but unsuccessfully.

Any ideas?

Thank you!

Re: Needing replacing a backslash by a comma by Paul

Paul
Fri May 09 13:42:53 CDT 2008


"Blosjos" <Blosjos@discussions.microsoft.com> wrote in message
news:49EBAA04-1A7F-4D56-89A0-96071A0688BE@microsoft.com...
> Hello,
>
> I just need to know how to find the third backslash in a string like
> this:
>
> \\192.168.1.2\SYSVOL
>
> It is like it was really \\anything\anything
>
> and then replace it by a comma so that the string can be like:
>
> \\192.168.1.2,SYSVOL
>
> I have been trying with regular expressions but unsuccessfully.
>
> Any ideas?

In many languages, lookbehinds and lookaheads would help solve the
problem, but VBS regular expressions can only do this for fixed string
patterns, not patterns with qualifiers.

If the second anything does not include backslashes, InstrRev will
find the third backslash easily and use of some string functions could
cut and paste things to get what you want.

You might try the replace function, replacing all backslashes with
commas, then replace double commas with double backslashes.

-Paul Randall



Re: Needing replacing a backslash by a comma by Alex

Alex
Fri May 09 13:48:43 CDT 2008

Yeah, this is one that takes a bit of punishment to do since \ is an escape
character in a regex and you have to do a pattern match. The following demo
code works, and should work with anything of the form
\\xxxx\
where xxxx is one or more of any characters other than \.

data = "\\192.168.1.2\SYSVOL"
Set rx = new regexp
rx.Pattern = "^(\\\\[^\\]+)\\"
data = rx.Replace(data, "$1,")
WScript.Echo data

Here's what the pattern does.
The initial ^ anchors the pattern to the beginning of the line; if your text
begins with spaces ever, you may need to remove that.
The \\\\ matches \\, since each literal \ needs to be escaped with a \. The
[^\\]+ bit means "one or more of any character _except_ \". The surrounding
() tells the regex engine that this is a submatch to capture. Since it's the
first captured match, the regex engine will remember it as $1 ($0 is
reserved for the entire match, captured or not).
Finally, the terminal \\ matches a single \.

In your example, the regex engine would capture \\192.168.1.2, and the
replacement expression replaces the entire matched section - which includes
the terminal \ - with the capture followed by a ,.

"Blosjos" <Blosjos@discussions.microsoft.com> wrote in message
news:49EBAA04-1A7F-4D56-89A0-96071A0688BE@microsoft.com...
> Hello,
>
> I just need to know how to find the third backslash in a string like this:
>
> \\192.168.1.2\SYSVOL
>
> It is like it was really \\anything\anything
>
> and then replace it by a comma so that the string can be like:
>
> \\192.168.1.2,SYSVOL
>
> I have been trying with regular expressions but unsuccessfully.
>
> Any ideas?
>
> Thank you!
>
>

Re: Needing replacing a backslash by a comma by Alex

Alex
Fri May 09 18:09:00 CDT 2008

"Paul Randall" <paulr901@cableone.net> wrote in message
news:uZgBCTgsIHA.1872@TK2MSFTNGP04.phx.gbl...
>

> In many languages, lookbehinds and lookaheads would help solve the
> problem, but VBS regular expressions can only do this for fixed string
> patterns, not patterns with qualifiers.

I've never used those in regular expressions before, although I think I've
used some regex engines that support that. How does it differ from the
VBScript RE engine's behavior? Are lookbehind/lookahead something like a
non-capturing default?


Re: Needing replacing a backslash by a comma by ekkehard

ekkehard
Mon May 12 16:49:19 CDT 2008

Blosjos schrieb:
> Hello,
>
> I just need to know how to find the third backslash in a string like this:
>
> \\192.168.1.2\SYSVOL
>
> It is like it was really \\anything\anything
>
> and then replace it by a comma so that the string can be like:
>
> \\192.168.1.2,SYSVOL
>
> I have been trying with regular expressions but unsuccessfully.
>
> Any ideas?
>
> Thank you!
>
>
You could use code like this:

Dim aTests : aTests = Array( _
"\\192.168.1.2\SYSVOL" _
, "\\192.168.1.2\SYSVOL\x" _
, "\x\192.168.1.2\SYSVOL" _
)
Dim sTest
For Each sTest in aTests
' Replace(expression, find, replacewith[, start[, count[, compare]]])
WScript.Echo sTest, "=>", Replace( sTest, "\", ",", 3, 1, vbBinaryCompare )
Next

output:

=== replBackslash: replace third backslash ====
\\192.168.1.2\SYSVOL => 192.168.1.2,SYSVOL
\\192.168.1.2\SYSVOL\x => 192.168.1.2,SYSVOL\x
\x\192.168.1.2\SYSVOL => ,192.168.1.2\SYSVOL
=== replBackslash: 0 done (00:00:00) ==========

to check if using Replace is an easy/efficient way to solve your
problem.

Re: Needing replacing a backslash by a comma by ekkehard

ekkehard
Mon May 12 17:36:56 CDT 2008

ekkehard.horner schrieb:
> Blosjos schrieb:
>> Hello,
>>
>> I just need to know how to find the third backslash in a string like
>> this:
>>
>> \\192.168.1.2\SYSVOL
>>
>> It is like it was really \\anything\anything
>>
>> and then replace it by a comma so that the string can be like:
>>
>> \\192.168.1.2,SYSVOL
>>
>> I have been trying with regular expressions but unsuccessfully.
>>
>> Any ideas?
>>
>> Thank you!
>>
>>
> You could use code like this:
>
> Dim aTests : aTests = Array( _
> "\\192.168.1.2\SYSVOL" _
> , "\\192.168.1.2\SYSVOL\x" _
> , "\x\192.168.1.2\SYSVOL" _
> )
> Dim sTest
> For Each sTest in aTests
> ' Replace(expression, find, replacewith[, start[, count[, compare]]])
> WScript.Echo sTest, "=>", Replace( sTest, "\", ",", 3, 1,
> vbBinaryCompare )
> Next
>
> output:
>
> === replBackslash: replace third backslash ====
> \\192.168.1.2\SYSVOL => 192.168.1.2,SYSVOL
> \\192.168.1.2\SYSVOL\x => 192.168.1.2,SYSVOL\x
> \x\192.168.1.2\SYSVOL => ,192.168.1.2\SYSVOL
> === replBackslash: 0 done (00:00:00) ==========
>
> to check if using Replace is an easy/efficient way to solve your
> problem.

Looks like this way wasn't easy enough for me! To get the desired
output:

\\192.168.1.2\SYSVOL => \\192.168.1.2,SYSVOL

the Replace call should be:

WScript.Echo sTest, "=>", "\\" & Replace( sTest, "\", ",", 3, 1, vbBinaryCompare )

Re: Needing replacing a backslash by a comma by Todd

Todd
Mon May 12 17:48:01 CDT 2008

Blosjos wrote:
> Hello,
>
> I just need to know how to find the third backslash in a string like this:
>
> \\192.168.1.2\SYSVOL
>
> It is like it was really \\anything\anything
>
> and then replace it by a comma so that the string can be like:
>
> \\192.168.1.2,SYSVOL
>
> I have been trying with regular expressions but unsuccessfully.
>
> Any ideas?

I read you have been trying regular expressions not that they are required.

Assuming the string will never contain more than 3 backslashes, you could
use InStrRev() like this.

s = "\\192.168.1.2\SYSVOL"
ret = InStrRev(s, "\")
s = Left(s, ret-1) & "," & Mid(s, ret+1)
wscript.echo s


But if the string contains more than three backslashes at times, you could
try this.

s = "\\192.168.1.2\SYSVOL\myfolder"
count = 0
ret = 0
Do
ret = InStr(ret+1, s, "\")
If ret = 0 Then Exit Do
count = count + 1
If count = 3 Then
s = Left(s, ret-1) & "," & Mid(s, ret+1)
Exit Do
End If
Loop
wscript.echo s

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)



Re: Needing replacing a backslash by a comma by James

James
Tue May 13 07:32:50 CDT 2008

"Blosjos" <Blosjos@discussions.microsoft.com> wrote in message
news:49EBAA04-1A7F-4D56-89A0-96071A0688BE@microsoft.com...
> Hello,
>
> I just need to know how to find the third backslash in a string like this:
>
> \\192.168.1.2\SYSVOL
>
> It is like it was really \\anything\anything
>
> and then replace it by a comma so that the string can be like:
>
> \\192.168.1.2,SYSVOL
>
> I have been trying with regular expressions but unsuccessfully.

You could use a pattern like "(^\\\\.*?)\\(.*?$)", which would look for
'\\' at the beginning of the string (since \ is an escape character, you
have to escape it, thus doubling it up). It will capture this and then any
characters up to the next slash & then capture any characters after the
slash all the way to the end. The Replace method is then used to replace the
entire string with the first capture followed by a comma and then the second
capture. Give this sample a try:

Set oRegEx = CreateObject("VBScript.RegExp")
oRegEx.Pattern = "(^\\\\.*?)\\(.*?$)"
sExample = "\\192.168.1.2\SYSVOL"
sExample = oRegEx.Replace(sExample, "$1,$2")
WScript.Echo sExample