2

I have this text https://bitbucket.com/user/repo.git and I want to print repo, the content between / and .git, without including delimiters. I have this:

echo https://bitbucket.com/user/repo.git | grep -E -o '\/(.*?)\.git'

But it prints /repo.git. How can I print just repo?

2 Answers 2

2

Use the [^/]+(?=\.git$) pattern with -P option:

echo https://bitbucket.com/user/repo.git | grep -P -o '[^/]+(?=\.git$)'

See the online demo

The [^/]+(?=\.git$) pattern matches 1+ chars other than / that are followed with .git at the end of the string.

0
1

You can use sed to do that

echo https://bitbucket.com/user/repo.git | sed -e 's/^.\*\\/\\(.\*\\).git$/\1/g'

Not the answer you're looking for? Browse other questions tagged or ask your own question.