You need a regular expression to remove directories from the path:

file://localhost/D:/X/Y/1.txt file://localhost/D:/X/A/B/2.txt file://localhost/D:/X/Q/W/E/3.txt 

As a result, you need to get:

 file://localhost/D:/X/1.txt file://localhost/D:/X/2.txt file://localhost/D:/X/3.txt 

    1 answer 1

    For python, I don’t know how accurate the regular schedule should be, but on php like this:

     $first = "file://localhost/D:/X/Y/1.txt"; $first = "file://localhost/D:/X/A/B/2.txt"; $first = "file://localhost/D:/X/Q/W/E/3.txt"; /* В результате нужно получить: file://localhost/D:/X/1.txt file://localhost/D:/X/2.txt file://localhost/D:/X/3.txt */ echo preg_replace("/(file:\/\/localhost\/D:)\/(.*?)\/(?:.*)\/(.*)/i", "($1/$2/$3)", $first); 
    • How does it work ($ 1 / $ 2 / $ 3)? - Alexo
    • <pre> <code> "/ (file: \ / \ / localhost \ / D:) \ / (. *?) \ / (?:. *) \ / (. *) / i" </ code> < / pre> (x) Finds x and remembers. This is called “memory brackets”. (?: x) Finds x, but does not remember what was found. So here we see that $ 1 is the first bracket, $ 2 is the second bracket, $ 3 is the fourth bracket. And we skip the third bracket, since "said" that we do not need it. - lampa
    • Everything works And if you need to replace in XML: <path> file: // localhost / D: /X/Y/Z/1.txt </ path> It turns out so <path> file: // localhost / D: / X / path> - Alexo
    • echo preg_replace ("/ (file: \ / \ / localhost \ / D:) \ / (. *?) \ / (?:. *) \ / (. *?) (<\ /) / i", " $ 1 / $ 2 / $ 3 $ 4 ", $ first); - lampa
    • one
      I have already done so x = re.sub (pattern, r "<path> \ g <1> / \ g <2> / \ g <3> </ path>", f.read ()) It seems that needed) - Alexo