开发者

Using Standard REGEX to add a character at the end of a string

开发者 https://www.devze.com 2023-01-15 01:15 出处:网络
I need to add a backslash \"\\\" to a string before storing it开发者_开发技巧; e.g. source \"user1@domain1\" - is stored in two variables: userid and domain. For the domain variable, before storing i

I need to add a backslash "\" to a string before storing it开发者_开发技巧;

e.g. source "user1@domain1" - is stored in two variables: userid and domain. For the domain variable, before storing it, I want to add a backward slash to the end of the domain name e.g. "domain1\". How can this be done in regex?


With standard regex you can add anything with 'echo'. Just note for a backslash and other characters, you have to use an escape character, which happens to also be a backslash.

echo domain\\

To split the string, use a method described here. I'll post my favorite for your case.

string='user1@domain1'
IFS=@ read left right <<< "$string"
echo "$left"
echo "$right\\"

gives an output of the username on the first line, and 'domain1\' on the second.

0

精彩评论

暂无评论...
验证码 换一张
取 消