From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: AW Newsgroups: gmane.emacs.help Subject: Re: NON-trivial regular expression problem (could not find on google) Date: Sat, 18 Jan 2003 17:52:16 -0500 Organization: Bell Sympatico 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 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: main.gmane.org 1042930953 11925 80.91.224.249 (18 Jan 2003 23:02:33 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Sat, 18 Jan 2003 23:02:33 +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 18a1yy-00036D-00 for ; Sun, 19 Jan 2003 00:02:32 +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 18a1x4-0005MK-00 for gnu-help-gnu-emacs@m.gmane.org; Sat, 18 Jan 2003 18:00:34 -0500 User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.1) Gecko/20020829 X-Accept-Language: en-us, en Original-Newsgroups: comp.lang.lisp,comp.lang.awk,comp.unix.shell,gnu.emacs.help Original-Lines: 56 Original-NNTP-Posting-Host: 65.95.225.31 Original-X-Complaints-To: abuse@sympatico.ca Original-X-Trace: news20.bellglobal.com 1042930329 65.95.225.31 (Sat, 18 Jan 2003 17:52:09 EST) Original-NNTP-Posting-Date: Sat, 18 Jan 2003 17:52:09 EST Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!cyclone.bc.net!torn!webster!nf1.bellglobal.com!nf2.bellglobal.com!news20.bellglobal.com.POSTED!not-for-mail Original-Xref: shelby.stanford.edu comp.lang.lisp:103184 comp.lang.awk:24429 comp.unix.shell:139869 gnu.emacs.help:109157 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:5682 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:5682 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. > > 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. > > The regular expression for the first two cases is demonstrated by this > sed script although the lisp variants are identical. > > sed 's,\(^.*\)\(/\|"\)\([^/][^/]*/\.\./\)\(.*"\),\1\2\4,' > > The regex I use for DIRNAME is [^/]+ written above using * because > sed is without plus. > > I will follow all the cross-posted newsgroups. If you prefer, because > some people are allergic to any cross-posting, you can post your reply > in just one of the groups pertaining to your application, ie lisp/elisp/sed/awk. > > democrat > Using the shell is easier, but here goes. BEGIN { FS="/"} { for( i=2;i<=NF;i++) { if( $i == "." ) $i =""; if($i == "..") $(i-1) == ""; print; } You only owe two bucks coz I burned your signature for heat...