regex - How to match repeated characters using regular expression operator =~ in bash? -
i want know if string has repeated letter 6 times or more, using =~ operator.
a="aaaaaaazxc2" if [[ $a =~ ([a-z])\1{5,} ]]; echo "repeated characters" fi
the code above not work.
bash regex flavor i.e. ere doesn't support backreference in regex. ksh93
, zsh
support though.
as alternate solution, can using extended regex option in grep
:
a="aaaaaaazxc2" grep -qe '([a-za-z])\1{5}' <<< "$a" && echo "repeated characters" repeated characters
edit: ere implementations support backreference extension. example ubuntu 14.04
supports it. see snippet below:
$> echo $bash_version 4.3.11(1)-release $> a="aaaaaaazxc2" $> re='([a-z])\1{5}' $> [[ $a =~ $re ]] && echo "repeated characters" repeated characters
Comments
Post a Comment