Re: Needing replacing a backslash by a comma



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@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:49EBAA04-1A7F-4D56-89A0-96071A0688BE@xxxxxxxxxxxxxxxx
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!


.



Relevant Pages

  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... match per string for either Regex. ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ... The engine will again try to see if the next character is a B. It ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex Capture problem
    ... "learned" my regex using a freeware utility that had slightly different ... was trying to capture instead of. ... I have used Regex utilities before, so I understand the concepts of text ... Function RESub(str As String, SrchFor As String, ReplWith As String) As String ...
    (microsoft.public.excel.programming)
  • Re: RegExp
    ... On a related note, the following regex is intended to capture "IN (23, ... Set myMatch = myMatches ... your regex will not do what you want. ... So, matching as much as it can, that will include all the digits except for the ...
    (microsoft.public.excel.programming)
  • Re: Regex Capture problem
    ... Here are two regex search and replace strings. ... RgxReplaceText = objRgx.Replace(strRgxInput, strRgxOutput) ... capture and re-use in a Regex expression. ...
    (microsoft.public.excel.programming)