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: AW
Subject: Re: NON-trivial regular expression problem (could not find on google)
Date: Sat, 18 Jan 2003 17:52:16 -0500
User-agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.1) Gecko/20020829

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...




reply via email to

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