help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NON-trivial regular expression problem (could not find on google)


From: John W. Krahn
Subject: Re: NON-trivial regular expression problem (could not find on google)
Date: Sun, 19 Jan 2003 00:53:45 GMT

Instant Democracy wrote:
> 
> Regular expression facilities are slightly varied in
> sed
> awk
> lisp
> emacs-lisp
> Therefore these newsgroups can all contribute to the discussion.
> 
> A frequent problem involves simplifying a pathname. The string format we
> can expect to encounter is covered by the following three examples:
> 
> "dir.name/../dir/../file"
> "dir/../d2/../file.ext"
> "d1/d2/../../file.ext"
> 
> The "" are part of the string, and not just string delimiters. These
> strings are inside regular text on the line. The paths are never
> absolute so that you will not encounter "/d1/file.ext".
> 
> The task is to eliminate patterns such as
>     DIRNAME/../
> from the path because they are redundant.


$ perl -le'
@paths = qw( "dir.name/../dir/../file"
             "dir/../d2/../file.ext"
             "d1/d2/../../file.ext"
             "../../../file"
           );
for ( @paths ) {
    print;
    s%([^"]+)% local $_ = $1; 1 while s|[^/]+(?<!\.\.)/\.\./||g; $_ %e;
    print;
    }
'
"dir.name/../dir/../file"
"file"
"dir/../d2/../file.ext"
"file.ext"
"d1/d2/../../file.ext"
"file.ext"
"../../../file"
"../../../file"




John
-- 
use Perl;
program
fulfillment


reply via email to

[Prev in Thread] Current Thread [Next in Thread]