简体   繁体   中英

Bash String extraction

I have a really easy problem that I am not able to solve quickly. I must extract a substring from something like this:

a="www.himom.com/byebye"

I want to retrieve this:

"/byebye"

Actually, I am able to retrieve

"www.himom.com" with

echo ${a%/*}

Any suggestions?

尝试这个:

 a="www.himom.com/byebye"; echo "/${a##*/}"

尝试以下操作: echo /${a##*/}它删除以'/'结尾的最长子字符串。

To leave the slash in the string

shopt -s extglob
echo "${a##*([^/])}"

我是个懒人。解决方案是:

${a##*/}
$ a="www.himom.com/byebye" | echo ${a##*m}
/byebye

You can extract your desired substring with a regex:

a="www.himom.com/byebye"
echo `expr match "$a" '.*\(\/.*\)'`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM