我需要从一个给定的字符串中删除url。我唯一的解决办法是:
_url="http://download.enlightenment.org/rel/apps/econnman/econnman-1.1.tar.gz"
_l=${_url%.*/*} # http://download.enlightenment
_l=${#_l} # 29
_url=${_url:0:${l}+4} # http://download.enlightenment.org但这将失败在每3个字符长度TLD,这是错误的方式。
经常这样。
发布于 2016-11-06 21:40:35
您可以使用grep
$ echo "$_url" | grep -Eo '^http[s]?://[^/]+'
http://download.enlightenment.org您可以在正则表达式中使用expr:
$ echo `expr "$_url" : '\(http://[^/]*\)'`
http://download.enlightenment.org或者,使用awk
echo "$_url" | awk -F/ 'BEGIN{OFS=FS} {print $1 OFS OFS $3}'
http://download.enlightenment.org您可以使用cut
echo "$_url" | cut -d/ -f1-3
http://download.enlightenment.orgcut可能也是获取其余url的最容易的方法:
$ echo "$_url" | cut -d/ -f4-
rel/apps/econnman/econnman-1.1.tar.gz或者,完全内在的巴什:
$ [[ $_url =~ ^([^:]+://[^/]+)/?(.*)$ ]] && server="${BASH_REMATCH[1]}"
$ echo "$server"
http://download.enlightenment.org"${BASH_REMATCH[2]}"拥有其余的网址。
发布于 2016-11-06 21:14:49
我不知道这是否适用于bash,但它适用于pcre regex引擎。
(?<=:\/\/)(.*)(?=\/)查找第一个/和第二个/之间的所有文本。为https://google.com/工作,但不适用于google.com/或https://google.com。取决于你需要什么。
https://stackoverflow.com/questions/40454336
复制相似问题