I thought I had a good regex line below that works with tests I did in Regexbuddy, but doesn't seem to work in bash.
I need someone with much better knowledge of regex than me to help me out. ;)
The point is to do a basic test as to whether a string contains a remote host for rsync. So we're testing for something valid like username@host:/ or username@host:~/ (and I also assume ./ ?) ...
#!/bin/bash
test="foo@bar:/here/path/"
regex='^([\w-_.]*)@([\w-_.:]*):[~./]'
if [[ "${test}" =~ "${regex}" ]]; then
echo "yes, remote host"
else
echo "no, local"
fi
# filter for remote host by regex
# ^ begin at start of line, ( [ match underscore, word & number chars, dashes, fullstops ] in * repetition ) until first @ and then ( [ match underscore, word & number chars, dashes, fullstops, and colons] in * repetition ) until : and then at least [ ~ or . or / )
# so someone@host-whatever-123.com:/path/ will match
# someone_here123@192.168.0.1:~/path/ will match
# blah123.user@2001:db8:85a3:8d3:1319:8a2e:370:7348:./path/ will match
# user@wherever:path/ will not, and /anything@starting.com:with/a/slash will not match
# etc
Any ideas?