Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match repeated characters using regular expression operator =~ in bash?

Tags:

regex

bash

I want to know if a string has repeated letter 6 times or more, using the =~ operator.

a="aaaaaaazxc2"
if [[ $a =~ ([a-z])\1{5,} ]];
then
     echo "repeated characters"
fi

The code above does not work.

like image 684
Ladih Avatar asked Nov 01 '25 17:11

Ladih


2 Answers

BASH regex flavor i.e. ERE doesn't support backreference in regex. ksh93 and zsh support it though.

As an alternate solution, you can do it using extended regex option in grep:

a="aaaaaaazxc2"
grep -qE '([a-zA-Z])\1{5}' <<< "$a" && echo "repeated characters"

repeated characters

EDIT: Some ERE implementations support backreference as an extension. For 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
like image 78
anubhava Avatar answered Nov 03 '25 07:11

anubhava


[[ $var =~ $regex ]] parses a regular expression in POSIX ERE syntax.

See the POSIX regex standard, emphasis added:

BACKREF - Applicable only to basic regular expressions. The character string consisting of a character followed by a single-digit numeral, '1' to '9'.

Backreferences are not formally specified by the POSIX standard for ERE; thus, they are not guaranteed to be available (subject to platform-specific libc extensions) in bash's native regex syntax, thus mandating the use of external tools (awk, grep, etc).

like image 38
Charles Duffy Avatar answered Nov 03 '25 07:11

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!