Masao Liu
Masao Liu

Reputation: 769

Regex to trim off leading and trailing slashes and catch the rest

I am trying to catch URL paths to yield the portion without leading and ending slashes /. Empty input characters before or after the trimming should be matched. The desired regex will behave as follows:

input-string        captured-string
-----------------------------------
/a/b/c/             a/b/c               
/a/b/c              a/b/c               
/                   (empty)
(empty)             (empty)

I use echo /a/b/c/d | sed -nr 's=(/(.+?)/)?=\2=p' and its flavors as the test tools as suggested by gurus and notice that the following regular expressions fail to do the job:

regex           input-string    wrong capture
---------------------------------------------
(/(.+?)/)?      /a/b/c          a/bc
(/(.+?)/)       /a/b/c          a/bc
(/(.+?)/)       /a              (doesn't match)
(/(.+?)/?)      /a/b/c/         a/b/c/
(/([^/]+)/?)    /a/b/c          ab/c
(/([^/.+])/?)   /a/b/c          ab/c
/*(.*?)/*       /a/b/c/         a/b/c/

The alleged correct answer appears to not be working, either:

echo /a/b/c | sed -nr 's=/*(?<x>.*?)/*=\k<x>=p'

because it gives this error message:

sed: -e expression #1, char 23: Invalid preceding regular expression

Helps will be much appreciated.

Edit: As pointed out by CompuChip, I used wrong test tool sed which appears to be not supporting non-greedy modifiers. The actual regex engine I am using is boost::regex_match() which gives me correct results given regex such as /?(.*?)/?. So I would like to close this question.

Upvotes: 1

Views: 1179

Answers (2)

NeronLeVelu
NeronLeVelu

Reputation: 10039

if only this kind of entry (so not inside other string)

sed "s#$#/#;s#^[^/].*##;s#/*$##;s#^/##"

Don't avoid thing like //bad/path/

Upvotes: 0

jkshah
jkshah

Reputation: 11713

Try following sed

sed -r 's:^/|/$::'

Short Description

Match : ^/|/$ = ^/ or /$ i.e. leading and trailing slash

Replace : (empty) i.e. trim the match

Test

$ cat file
/a/b/c/
/a/b/c
/

$ sed -r 's:^/|/$::' file
a/b/c/
a/b/c

Upvotes: 1

Related Questions