rootx

[SOLVED] StringReplace Multiple BackSlash help

5 posts in this topic

#1 ·  Posted (edited)

Hi, I need I trick to remove all double or triple etc... backslash. Thx

$string = "I:\test\\xxx\jjj\\\kkk\k.jpg"

Mmmm, I found a way but if you have more elegant way, please post it!

$string = "I:\test\\xxx\jjj\\\kkk\k.jpg"
$root = StringTrimRight($string,StringLen($string)-3)

$aArray = _StringBetween($string, "\", "\")


For $i = UBound($aArray) - 1 To 0 Step -1
    If $aArray[$i] = "" Then
        _ArrayDelete($aArray, $i)
    EndIf
Next

$string2 = _ArrayToString($aArray,"\")
MsgBox("","",$root&$string2)

 

Edited by rootx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi.

I'm not sure if you really mean remove or mean replace woth only one "\". I guess second. So here is my try with StringRegExpReplace():

Local $string = "I:\test\\xxx\jjj\\\kkk\k.jpg"
$string = StringRegExpReplace($string, "\\+", "\\")
ConsoleWrite($string & @CRLF)

Explanation: searchstring - \\+ - first \ is only masking second\ (otherwise it has another function) and + means one till all \

Replace it with \\. Here the first \ is masking the second \ again.

Conrad

Edited by Simpel
add explanation
1 person likes this

SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

This should work.

$string = "I:\test\\xxx\jjj\\\kkk\k.jpg"
$string=StringRegExpReplace($string,"\\{2,}","\\")
ConsoleWrite($string & @CRLF)

Saludos

1 person likes this

Share this post


Link to post
Share on other sites

Difference between @Danyfirex and mine: My script replaces all existing \, \\, \\\ ... with \ and Danyfirex' starts with minimum 2 \\ - so \\,\\\ ...

Conrad


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

THX Guys!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now