Closed Thread Icon

Topic awaiting preservation: perl directories RE (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=13024" title="Pages that link to Topic awaiting preservation: perl directories RE (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: perl directories RE <span class="small">(Page 1 of 1)</span>\

 
ninmonkey
Nervous Wreck (II) Inmate

From:
Insane since: Nov 2003

posted posted 01-02-2004 19:40

I have a list of directories that I need to change from /foo/bar to ../../ (for each folder, I want to replace the scalar with ../ )

What is the simplest RE to do this? I was thinking something like s/(\/.*)(.*)/..\/${2}/i;

But that would probably turn /foo/bar into ../ since perl is "greedy" Is there a better way that "\/[all chars except/]*(.*)"

If that makes sense, thanks for the help.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 01-02-2004 21:52

to quote perl.com http://www.perl.com/doc/manual/html/pod/perlre.html

quote:
By default, a quantified subpattern is ``greedy'', that is, it will match as many times as possible (given a particular starting location) while still allowing the rest of the pattern to
match. If you want it to match the minimum number of times possible, follow the quantifier with a ``?''. Note that the meanings don't change, just the ``greediness''



All clear?

ninmonkey
Nervous Wreck (II) Inmate

From:
Insane since: Nov 2003

posted posted 01-03-2004 02:37

That looks like what I need, but I am still having trouble. I think I am overcomplicating the RE. I want to replace ever /foo with ../

Here's what I have, but, it only prepends the directory with ../

code:
$img_path =~ s/(\/.*?)(.*)/\.\.\/$2/g;

thanks

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 01-03-2004 10:56

well... Why do you do this with regexps anyhow.
Why don't you do a 'while there's a / in string' 'cut off till next slash' add '../' to new string, when done add rest to new string?

« BackwardsOnwards »

Show Forum Drop Down Menu