Jump to content

A better pattern (SRER)


Deye
 Share

Recommended Posts

@trying to replace the "C:\EEC\"  if its to be the $sSearch instance

with the others it mostly works .. :huh2:

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

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 by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

 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 by Deye
Link to comment
Share on other sites

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 by Deye
Link to comment
Share on other sites

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 by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

 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 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 by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

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.

#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 by Malkey
Link to comment
Share on other sites

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', ''))

 

Link to comment
Share on other sites

  • 4 weeks later...

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 by Deye
Link to comment
Share on other sites

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)

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...