Deye Posted December 29, 2019 Share Posted December 29, 2019 @trying to replace the "C:\EEC\" if its to be the $sSearch instance with the others it mostly works .. Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC\' & @CRLF & _ 'C:\MSfree\' & @CRLF $sSearch = "C:\MSfree" ;~ $sSearch = "C:\EEC" $sOutput = StringRegExpReplace($Paths, "(\Q" & $sSearch & "\E\R)|(\Q" & $sSearch & "\E\\\R)", "") MsgBox("", "", $sOutput) Thanks Deye Link to comment Share on other sites More sharing options...
iamtheky Posted December 29, 2019 Share Posted December 29, 2019 (edited) Fun SRER problem aside, if you want a literal replacement what edge cases are preventing you from just using stringreplace? something like: Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC\' & @CRLF & _ 'C:\MSfree\' & @CRLF ;~ $sSearch = "C:\MSfree\" $sSearch = "C:\EEC\" $sOutput = stringstripws(StringReplace($Paths, $sSearch , "") , 4) MsgBox("", "", $sOutput) Edited December 29, 2019 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Deye Posted December 29, 2019 Author Share Posted December 29, 2019 (edited) if $Paths has a line that contains "C:\MSfree\someother file" Then _stingreplace will leave me with a line of "\someother file" .etc also i may have the search instance with\out "\" to the end maybe something that braces a pattern at first can do the job: ? $sPattern = "(" & StringReplace(StringRegExpReplace($sSearch, "[][$^.{}()+\\\-:]", "\\$0"), "*", ".*?") & "\\\R)" ;& "$" Still didn't get it to work as yet .. Edited December 29, 2019 by Deye Link to comment Share on other sites More sharing options...
Deye Posted December 29, 2019 Author Share Posted December 29, 2019 (edited) not sure how will this work with other unknown cases of file names but this does it for now : can turn messy later .. Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC\' & @CRLF & _ 'C:\MSfree\' & @CRLF $sSearch = "C:\EEC\" $sPattern = "(?:" & StringReplace(StringRegExpReplace($sSearch, "[][$^.{}()+\\\-:]", "\\$0"), "*", ".*?") ;& "$" $sOutput = StringRegExpReplace($Paths, $sPattern & "\R)|" & $sPattern & "\\\R)", "") MsgBox("", "", $sOutput) Edited December 29, 2019 by Deye Link to comment Share on other sites More sharing options...
iamtheky Posted December 29, 2019 Share Posted December 29, 2019 (edited) Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC\someotherfolder\' & @CRLF & _ 'C:\MSfree\' & @CRLF $sSearch = "C:\EEC\" ;~ $sSearch = "C:\MSfree\" $sOutput = StringRegExpReplace($Paths , stringreplace($sSearch , "\" , "\\") & ".*\R" , "") MsgBox("", "", $sOutput) maybe just ensuring the backslashes are double-upped within the regex? Edited December 29, 2019 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Deye Posted December 29, 2019 Author Share Posted December 29, 2019 basically if "C:\EEC (1)" doesn't exist in a longer version like "C:\EEC (1)\someother file" then it needs to be removed I will need to pay attention on to how the If StringRegExp($Paths, $sPattern & "\\)+[\w\d_]") should work - to detect if there are longer lines of the search instance if there aren't any only then should the instance get removed Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC (1)\' & @CRLF & _ 'C:\EEC (1)\someother file' & @CRLF & _ 'C:\MSfree\' & @CRLF $sSearch = "C:\EEC (1)" $sPattern = "(?:" & StringReplace(StringRegExpReplace($sSearch, "[][$^.{}()+\\\-:]", "\\$0"), "*", ".*?") If StringRegExp($Paths, $sPattern & "\\)+[\w\d_]") Then Else $sOutput = StringRegExpReplace($Paths, $sPattern & "\R)|" & $sPattern & "\\\R)", "") MsgBox("", "", $sOutput) EndIf Link to comment Share on other sites More sharing options...
iamtheky Posted December 29, 2019 Share Posted December 29, 2019 (edited) more fun than i had gathered... i'd probably use stringreplace to test the number of times it appears, then act off the value in extended Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC (1)\' & @CRLF & _ 'C:\EEC (1)\someother file' & @CRLF & _ 'C:\MSfree\' & @CRLF $sSearch = "C:\EEC (1)\" stringreplace($Paths , $sSearch , $sSearch) $sOutput = @Extended = 1 ? StringStripWS(stringreplace($Paths , $sSearch , "") , 4) : @Extended & " matches for: " & $sSearch MsgBox("", "", $sOutput) Edited December 29, 2019 by iamtheky Deye 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Deye Posted December 29, 2019 Author Share Posted December 29, 2019 Thanks iamtheky I will give it a little more time to see what else crept s up with later on tests Link to comment Share on other sites More sharing options...
Malkey Posted December 30, 2019 Share Posted December 30, 2019 (edited) 8 hours ago, Deye said: basically if "C:\EEC (1)" doesn't exist in a longer version like "C:\EEC (1)\someother file" then it needs to be removed .. .... Here are two methods to literalize metacharacters in the $sSearch variable. Either using "\Q...\E", or, escaping all metacharacters. expandcollapse popup#include <Array.au3> Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC (1)\someother file' & @CRLF & _ 'C:\MSfree\' & @CRLF & _ 'C:\EEC (1)\' Local $sSearch = "C:\EEC (1)" ; --------------------- Method 1 ---------------------------------- ; Check if "\E" is in $sSearch. If "\E" is present, replace "\E" with "\E\\E\Q", because of the "\Q" & $sSearch & "\E" in RE pattern. Local $sSearchA = (StringInStr($sSearch, "\E") ? StringReplace($sSearch, "\E", "\E\\E\Q") : $sSearch) ;ConsoleWrite("\Q" & $sSearchA & "\E" & @CRLF) Local $a = StringRegExp($Paths, "(\Q" & $sSearchA & "\E.*)", 3) _ArraySort($a) ;_ArrayDisplay($a) If UBound($a) > 1 Then For $i = 1 To UBound($a) - 1 ; Keep $a1[0] $sSearchB = (StringInStr($a[$i], "\E") ? StringReplace($a[$i], "\E", "\E\\E\Q") : $a[$i]) ;ConsoleWrite("\Q" & $sSearchB & "\E" & @CRLF) Local $sOutput = StringRegExpReplace($Paths, "(\Q" & $sSearchB & "\E\R?)", "") Next Else Local $sOutput = StringRegExpReplace($Paths, "(\Q" & $sSearchA & "\E.*\R?)", "") EndIf MsgBox(0, "\Q...\E", $sOutput) ; Or ; --------------------- Method 2 ---------------------------------- ; Beware "(1)" = (n) - Tests whether the capturing group with absolute number n matched. $sSearch = StringRegExpReplace($sSearch, "([\\()\.^$|\[\]{}*+?#])", "\\$1") ;ConsoleWrite($sSearch & @CRLF) Local $a1 = StringRegExp($Paths, "(" & $sSearch & ".*)", 3) _ArraySort($a1) ;_ArrayDisplay($a1) If UBound($a1) > 1 Then For $i = 1 To UBound($a1) - 1 ; Keep $a1[0] $sSearchC = StringRegExpReplace($a1[$i], "([\\()\.^$|\[\]{}*+?#])", "\\$1") ;ConsoleWrite($sSearchC & @CRLF) Local $sOutput = StringRegExpReplace($Paths, "(" & $sSearchC & "\R?)", "") Next Else Local $sOutput = StringRegExpReplace($Paths, "(" & $sSearch & ".*\R?)", "") EndIf MsgBox(0, "\\,(,)", $sOutput) #cs ; Both methods return:- C:\Users\ C:\MSfree\ C:\EEC (1)\ #ce Edited December 30, 2019 by Malkey Deye 1 Link to comment Share on other sites More sharing options...
Nine Posted December 30, 2019 Share Posted December 30, 2019 That seems to be working fine : #include <Constants.au3> Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC (1)\someother file' & @CRLF & _ 'C:\MSfree\' & @CRLF & _ 'C:\EEC (1)\' & @CRLF & _ 'C:\EEC (1)\EEC' & @CRLF & _ 'C:\Temp\' Local $sSearch = "C:\EEC (1)\" $sSearch = StringReplace($sSearch, "\E", "\E\\E\Q") MsgBox ($MB_SYSTEMMODAL,"",StringRegExpReplace ($Paths, '(?m)^(\Q' & $sSearch & '\E.*)$\R', '')) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Deye Posted December 30, 2019 Author Share Posted December 30, 2019 Yes, Thanks Malkey that solves the issue to My satisfactory Link to comment Share on other sites More sharing options...
Deye Posted January 23, 2020 Author Share Posted January 23, 2020 (edited) Still no relief : #include <Constants.au3> Local $Paths = 'H:\Users\' & @CRLF & _ 'H:\EEC (1)\someother file' & @CRLF & _ 'H:\MSfree\' & @CRLF & _ 'H:\QQEC (1)\' & @CRLF & _ 'H:\EEC (1)\EEC' & @CRLF & _ 'H:\Temp\' Local $sSearch = "H:\" $sSearch = '\Q' & StringReplace($sSearch, "\E", "\E\\E\Q") & "\E" MsgBox($MB_SYSTEMMODAL, "", StringRegExpReplace($Paths, $sSearch & ".*\R", '')) MsgBox($MB_SYSTEMMODAL, "", StringRegExpReplace($Paths, $sSearch & ".*\R", '')) must i remove them one by one as with Malkey's example ? Edit: NM, was getting ahead of my self Its: $sSearch = '\Q' & StringReplace($sSearch, "\E", "\E\\E\Q") & "\E" $Paths = StringRegExpReplace($Paths, $sSearch & ".*\R", '') MsgBox($MB_SYSTEMMODAL, "", $Paths) $Paths = StringRegExpReplace($Paths, $sSearch & ".*", '') MsgBox($MB_SYSTEMMODAL, "", $Paths) Edited January 23, 2020 by Deye Link to comment Share on other sites More sharing options...
jguinch Posted January 23, 2020 Share Posted January 23, 2020 More simple : Local $Paths = 'C:\Users\' & @CRLF & _ 'C:\EEC\' & @CRLF & _ 'C:\MSfree\' & @CRLF $sSearch = "c:\EEC\" $sOutput = StringRegExpReplace($Paths, "(?i)\Q" & StringLower($sSearch) & "\E\R", "") MsgBox(0, "", $sOutput) Deye 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Deye Posted January 24, 2020 Author Share Posted January 24, 2020 that is true Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now