2016-12-30 33 views
1

我有這個文本https://bitbucket.com/user/repo.git,我想打印repo,內容爲/.git,不包括分隔符。我有這個:在grep中打印匹配正則表達式組

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

但它打印/repo.git。我如何才能打印repo

回答

2

-P選項使用[^/]+(?=\.git$)模式:

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

online demo

[^/]+(?=\.git$)模式匹配比/其他1+字符被字符串的結束,隨後用.git

+1

謝謝,它的工作原理。 –

1

您可以使用sed

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