|
From: | Dr. Yuan Liu |
Subject: | Re: NON-trivial regular expression problem (could not find on google) |
Date: | Sun, 19 Jan 2003 00:36:12 GMT |
User-agent: | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 |
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.
It turns out to be trivial in both cases.> The real problem is constructing a regular expression for the DIRNAME before the /..
In order to illustrate your question, I reconstructed test data to be non-trivial. The solution is just one regexp with one loop in sed, ':2dots; s;[^/]*/\.\./;;g; t2dots':
$ sed ':2dots; s;[^/]*/\.\./;;g; t2dots' <<EOM # test data ends with EOM d01/dir.name/../dir/../file d02/dir/../d2/../file.ext1 d03/d1/d2/../../file.ext2 EOM d01/file d02/file.ext1 d03/file.ext2 Just learned this loop trick from John L. a couple of days ago:-) Yuan Liu
[Prev in Thread] | Current Thread | [Next in Thread] |