From: Larry Clapp <larry@theclapp.org>
Subject: Re: NON-trivial regular expression problem (could not find on google)
Date: Sat, 18 Jan 2003 19:27:24 -0500 [thread overview]
Message-ID: <cdrc0b.4iu.ln@127.0.0.1> (raw)
In-Reply-To: 7606630f.0301181219.60384da2@posting.google.com
In article <7606630f.0301181219.60384da2@posting.google.com>, Instant Democracy wrote:
> 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.
>
> For lines which do not have ../.. in them, this is trivial, for
> example by regexp in sed, emacs etc.
>
> The real problem is constructing a regular expression for the
> DIRNAME before the /..
>
> This DIRNAME can be described as a string that contains neither
> / not double-dot but anything else. Perhaps I am overlooking
> something else about DIRNAME.
Yes. "../.." doesn't matter. Find any pathname component
followed by ".." and remove both. Continue until no more ".."'s
exist in the string. In Perl:
$_ = "d1/d2/../../file.ext";
do {} while (s#[^/]+/\.\./##g);
print $_;
In Common Lisp:
(defun split (s delim)
(let ((start-at 0))
(nconc
(loop for c across s
and i upto (length s)
nconc (when (eql c delim)
(prog1
(list (subseq s start-at i))
(setf start-at (1+ i)))))
(list (subseq s start-at)))))
(defun join (list delim)
(let ((res (car list))
(delim (make-string 1 :initial-element delim)))
(dolist (el (cdr list))
(setf res (concatenate 'string res delim el)))
res))
(defun normalize-path (path)
(let ((names (split path #\/))
(stack nil))
(dolist (name names)
(cond ((string= name "..") (pop stack))
(t (push name stack))))
(join (reverse stack) #\/)))
Testing:
(let ((paths '("dir.name/../dir/../file"
"dir/../d2/../file.ext"
"d1/d2/../../file.ext"
"1234/../2345/../3546/3456/3456/../../asdf/asdf/file")))
(dolist (path paths)
(print (normalize-path path))))
-> "file"
-> "file.ext"
-> "file.ext"
-> "3546/asdf/asdf/file"
=> NIL
> [...]
> sigfile - you are welcome to use it or modify without changing
> its thrust.
Please drop this 3k sigfile. Thank you.
-- Larry
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
next prev parent reply other threads:[~2003-01-19 0:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-01-18 20:19 NON-trivial regular expression problem (could not find on google) Instant Democracy
2003-01-18 21:38 ` Harry Putnam
2003-01-18 21:46 ` Kai Großjohann
2003-01-18 22:52 ` AW
2003-01-18 23:03 ` Edi Weitz
2003-01-19 0:27 ` Larry Clapp [this message]
2003-01-19 0:36 ` Dr. Yuan Liu
2003-01-19 0:53 ` John W. Krahn
2003-01-19 1:05 ` William Park
2003-01-19 12:17 ` Peter J. Acklam
2003-01-19 19:26 ` Dr. Yuan Liu
2003-01-20 9:15 ` Peter J. Acklam
2003-01-24 18:56 ` Yuan Liu
2003-01-24 11:20 ` Bruce Barnett
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cdrc0b.4iu.ln@127.0.0.1 \
--to=larry@theclapp.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.