From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Larry Clapp Newsgroups: gmane.emacs.help Subject: Re: NON-trivial regular expression problem (could not find on google) Date: Sat, 18 Jan 2003 19:27:24 -0500 Organization: Newsfeeds.com http://www.newsfeeds.com 80,000+ UNCENSORED Newsgroups. Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: <7606630f.0301181219.60384da2@posting.google.com> NNTP-Posting-Host: main.gmane.org X-Trace: main.gmane.org 1042936149 24670 80.91.224.249 (19 Jan 2003 00:29:09 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Sun, 19 Jan 2003 00:29:09 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 18a3KZ-0006P7-00 for ; Sun, 19 Jan 2003 01:29:07 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 18a3Lw-0007PT-03 for gnu-help-gnu-emacs@m.gmane.org; Sat, 18 Jan 2003 19:30:20 -0500 Original-Newsgroups: comp.lang.lisp,comp.lang.awk,comp.unix.shell,gnu.emacs.help Original-Followup-To: comp.lang.lisp User-Agent: slrn/0.9.7.4 (Linux) Original-NNTP-Posting-Host: 65.222.106.138 Original-X-Trace: corp.newsgroups.com 1042935982 65.222.106.138 (18 Jan 2003 18:26:22 -0600) Original-Lines: 91 X-Comments: This message was posted through Newsfeeds.com X-Comments2: IMPORTANT: Newsfeeds.com does not condone, nor support, spam or any illegal or copyrighted postings. X-Comments3: IMPORTANT: Under NO circumstances will postings containing illegal or copyrighted material through this service be tolerated!! X-Report: Please report illegal or inappropriate use to X-Abuse-Info: Please be sure to forward a copy of ALL headers, INCLUDING the body (DO NOT SEND ATTACHMENTS) Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!cyclone.bc.net!news-out.newsfeeds.com!l2!corp.newsgroups.com!127.0.0.1!nobody Original-Xref: shelby.stanford.edu comp.lang.lisp:103187 comp.lang.awk:24431 comp.unix.shell:139872 gnu.emacs.help:109159 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:5684 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:5684 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! =-----