If I understand this correctly, ^(\S?)(\S?)(\S?)\S?\3\2\1$
matches poj
, kip
and ret
by capturing empty in the capture groups. Disclaimer: I'm not 100% sure this is the correct conclusion.
In order to mandate at least 2 characters, make the first capture mandatory:
^(\S)(\S?)(\S?)\S?\3\2\1$
.
Example:
#!/bin/bash
teststrs=('aa' 'abccba' 'abba' 'abcba' 'abcdcba' 'abcdcbaaaaa'
'ab' 'abc cba' 'poj' 'kip' 'ret')
re='^(\S)(\S?)(\S?)\S?\3\2\1$'
for str in "${teststrs[@]}"
do
if [[ "$str" =~ $re ]]; then
echo "$str matches"
fi
done
Output:
aa matches
abccba matches
abba matches
abcba matches
abcdcba matches
You'll get a similar result for grep -E '^(\S)(\S?)(\S?)\S?\3\2\1$'
, given this input:
aa
abccba
abba
abcba
abcdcba
abcdcbaaaaa
ab
abca
abc cba
poj
kip
ret
You'll get this:
aa
abccba
abba
abcba
abcdcba
If you instead use your original, grep -E '^(\S?)(\S?)(\S?)\S?\3\2\1$'
, you'll get the three letter matches you talked about too, but using the grep --color
option shows no actual matches on the four last ones:
aa
abccba
abba
abcba
abcdcba
abca
poj
kip
ret