简体   繁体   中英

Bash regex match after @

I want to match a string after @ For example : stringZ="-p 2234 root@tor"

the result would be "tor"

The best I managed to do is : echo expr match "$stringZ" '\\(.*\\@\\)' which output: -p 2234 root@ How can I negate this?

You could just use the bash builtin string modification functionality:

echo "${stringZ##*@}"

Doesn't even require a regex, because, really what you are trying to do doesn't need one...

You can use sed :

echo '-p 2234 root@tor' | sed -r 's/.*@//'

Or if you had it in the stringZ variable:

newstring=$(echo stringZ | sed -r 's/.*@//')
echo $newstring

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